Lanka Developers Community

    Lanka Developers

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Shop
    1. Home
    2. nb
    3. Best
    • Profile
    • Following 0
    • Followers 0
    • Topics 0
    • Posts 2
    • Best 1
    • Controversial 0
    • Groups 0

    Best posts made by nb

    • RE: Typescrypt array help.

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

      posted in Front-End Development
      nb
      nb
    • 1 / 1