Lanka Developers Community

    Lanka Developers

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Shop

    Typescrypt array help.

    Front-End Development
    typescript angular
    2
    3
    407
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • oditha
      oditha Web Development last edited by

      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 }
                                ]
      

      ස්තූතියි

      1 Reply Last reply Reply Quote 0
      • nb
        nb last edited by

        මේ වැඩේ හිතුවට වඩා සංකීර්ණයි. කොහොම හරි 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 එක නං හරි.

        Playground link

        oditha 1 Reply Last reply Reply Quote 2
        • oditha
          oditha Web Development @nb last edited by

          @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

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post

          2
          Online

          3.7k
          Users

          1.3k
          Topics

          5.3k
          Posts

          • Privacy
          • Terms & Conditions
          • Donate

          © Copyrights and All right reserved Lanka Developers Community

          Powered by Axis Technologies (PVT) Ltd

          Made with in Sri Lanka

          | |