Typescrypt array help.
-
Parent array
let activity = [ { startDate: 2020-10-21, endDate: 2020-10-22, steps: 100, distance: 100 }, { startDate: 2020-10-22, endDate: 2020-10-23, steps: 100, distance: 100 } ]
මෙන්න මේ උඩ තියෙන Array එකට පහත දක්වලා තියෙන arrays 2න් ඩේටා ගන්න ඕන. මේ උඩ තියෙන ඇරේ එකේ startDate, endDate වලට සමාන අගයන් තියෙන පහල තියෙන ඇරෙස් වලින් ඩේටා අරගෙන ෆිල් කරවන්න ඕන. මේක කොහොමද කරගන්නෙ?
let steps= [ { startDate: 2020-10-21, endDate: 2020-10-22, steps: 100 }, { startDate: 2020-10-22, endDate: 2020-10-23, steps: 100 } ]
let activity = [ { startDate: 2020-10-21, endDate: 2020-10-22, distance: 100 }, { startDate: 2020-10-22, endDate: 2020-10-23, distance: 100 } ]
ස්තූතියි
-
මේ වැඩේ හිතුවට වඩා සංකීර්ණයි. කොහොම හරි O(n2) solution එකක් හොයා ගත්තා.
interface Steps extends Record<string, any> { startDate: String; endDate: String; steps: Number } interface Distance extends Record<string, any> { startDate: String; endDate: String; distance: Number } let steps: Array<Steps> = [ { startDate: '2020-10-21', endDate: '2020-10-22', steps: 101 }, { startDate: '2020-10-22', endDate: '2020-10-23', steps: 102 }, { startDate: '2020-10-24', endDate: '2020-10-25', steps: 103 } ]; let activity: Array<Distance> = [ { startDate: '2020-10-21', endDate: '2020-10-22', distance: 101 }, { startDate: '2020-10-22', endDate: '2020-10-23', distance: 102 }, { startDate: '2020-10-26', endDate: '2020-10-26', distance: 103 } ]; function join<L extends Record<string,any>, R extends Record<string,any>>(left: Array<L>, right: Array<R>, by: Array<string>) { const joined: Array<L&R> = []; for (let l of left) { const fromRight = right.find(r => by.every((curr) => (r[curr] == l[curr]), true)) if (fromRight === undefined) continue; joined.push({ ...fromRight, ...l}) } return joined; } console.log(join(steps, activity, ['startDate', 'endDate'])) /* [LOG]: [{ "startDate": "2020-10-21", "endDate": "2020-10-22", "distance": 101, "steps": 101 }, { "startDate": "2020-10-22", "endDate": "2020-10-23", "distance": 102, "steps": 102 }] */
Types දාපු හැටි හරිද දන්නෑ. logic එක නං හරි.
-
@nb said in Typescrypt array help.:
distance: Number } let steps: Array<Steps> = [ { startDate: '2020-10-21', endDate: '2020-10-22', steps: 101 }, { startDate: '2020-10-22', endDate: '2020-10-23', steps: 102 }, { startDate: '2020-10-24', endDate: '2020-10-25', steps: 103 } ]; let activity: Array<Distance> = [ { startDate: '2020-10-21', endDate: '2020-10-22', distance: 101 }, { startDate: '2020-10-22', endDate: '2020-10-23', distance: 102 }, { startDate: '2020-10-26', endDate: '2020-10-26', distance: 103 } ]; function join<L extends Record<string,any>, R extends Record<string,any>>(left: Array<L>, right: Array<R>, by: Array<string>) { const joined: Array<L&R> = []; for (let l of left) { const fromRight = right.find(r => by.every((curr) => (r[curr] == l[curr]), true)) if (fromRight === undefined) continue; joined.push({ ...fromRight, ...l}) } return joined; } console.log(join(steps, activity, ['startDate', 'endDate'])) /* [LOG]: [{ "startDate": "2020-10-21", "endDate": "2020-10-22", "distance": 101, "steps": 101 }, { "startDate": "2020-10-22", "endDate": "2020-10-23", "distance": 102, "steps": 102 }] */
Thanx Brother. man try karala balannam meka