Home > Software engineering >  Remove duplicates from array by keeping 1st array data in typescript
Remove duplicates from array by keeping 1st array data in typescript

Time:12-01

I have 2 arrays. I need to merge both the arrays but output should contains all records from arr1 and only unique records from arr2. key we can consider "number".

Please let me know best way to get the output. Basically I need to merge and get the unique "number" but result should contain all records from arr1 and only unique records from arr2.

let arr1 = [
  {
    "number": "1234",
    "Name": "test",    
    "data": [
      {
        "qty": 0,
        "val": 1.11
      },
      {
        "qty": 500,
        "val": 2.92
      },
      {
        "qty": 1000,
        "val": 2.84
      },
      {
        "qty": 1500,
        "val": 2.66
      }
    ]
  },
    {
    "number": "6776",
    "Name": "test9",    
    "data": [
      {
        "qty": 0,
        "val": 2
      },
      {
        "qty": 100,
        "val": 3
      },
      {
        "qty": 200,
        "val": 4
      },
      {
        "qty": 300,
        "val": 5
      }
    ]
  },
  
]

let arr2 = [
{
    "number": "1234",
    "Name": "test",    
    "data": [
      {
        "qty": 0,
        "val": 1.11
      }
    ]
  },
  {
    "number": "7896",
    "Name": "test4",    
    "data": [
      {
        "qty": 0,
        "val": 5.11
      },
      {
        "qty": 500,
        "val": 6.92
      },
      {
        "qty": 1000,
        "val": 3.84
      },
      {
        "qty": 1500,
        "val": 1.66
      }
    ]
  },
  {
    "number": "4567",
    "Name": "test2",    
    "data": [
      {
        "qty": 0,
        "val": 4.11
      },
      {
        "qty": 500,
        "val": 9.92
      },
      {
        "qty": 1000,
        "val": 5.84
      },
      {
        "qty": 1500,
        "val": 7.66
      }
    ]
  }
]


After merging above 2 arrays I need the below output:

[
  {
    "number": "1234",
    "Name": "test",    
    "data": [
      {
        "qty": 0,
        "val": 1.11
      },
      {
        "qty": 500,
        "val": 2.92
      },
      {
        "qty": 1000,
        "val": 2.84
      },
      {
        "qty": 1500,
        "val": 2.66
      }
    ]
  },
    {
    "number": "6776",
    "Name": "test9",    
    "data": [
      {
        "qty": 0,
        "val": 2
      },
      {
        "qty": 100,
        "val": 3
      },
      {
        "qty": 200,
        "val": 4
      },
      {
        "qty": 300,
        "val": 5
      }
    ]
  },
  {
    "number": "7896",
    "Name": "test4",    
    "data": [
      {
        "qty": 0,
        "val": 5.11
      },
      {
        "qty": 500,
        "val": 6.92
      },
      {
        "qty": 1000,
        "val": 3.84
      },
      {
        "qty": 1500,
        "val": 1.66
      }
    ]
  },
  {
    "number": "4567",
    "Name": "test2",    
    "data": [
      {
        "qty": 0,
        "val": 4.11
      },
      {
        "qty": 500,
        "val": 9.92
      },
      {
        "qty": 1000,
        "val": 5.84
      },
      {
        "qty": 1500,
        "val": 7.66
      }
    ]
  }
  
]

CodePudding user response:

create a key Set collection so you can check if it already exists and filter out from arr2.

arr1Set =  new Set(arr1.map(o => o.number));
let newArry = arr1.concat(...arr2.filter(o => !arr1KeySet.has(o.number)))

let arr1 = [
  {
    "number": "1234",
    "Name": "test",    
    "data": [
      {
        "qty": 0,
        "val": 1.11
      },
      {
        "qty": 500,
        "val": 2.92
      },
      {
        "qty": 1000,
        "val": 2.84
      },
      {
        "qty": 1500,
        "val": 2.66
      }
    ]
  },
    {
    "number": "6776",
    "Name": "test9",    
    "data": [
      {
        "qty": 0,
        "val": 2
      },
      {
        "qty": 100,
        "val": 3
      },
      {
        "qty": 200,
        "val": 4
      },
      {
        "qty": 300,
        "val": 5
      }
    ]
  },
  
]

let arr2 = [
{
    "number": "1234",
    "Name": "test",    
    "data": [
      {
        "qty": 0,
        "val": 1.11
      }
    ]
  },
  {
    "number": "7896",
    "Name": "test4",    
    "data": [
      {
        "qty": 0,
        "val": 5.11
      },
      {
        "qty": 500,
        "val": 6.92
      },
      {
        "qty": 1000,
        "val": 3.84
      },
      {
        "qty": 1500,
        "val": 1.66
      }
    ]
  },
  {
    "number": "4567",
    "Name": "test2",    
    "data": [
      {
        "qty": 0,
        "val": 4.11
      },
      {
        "qty": 500,
        "val": 9.92
      },
      {
        "qty": 1000,
        "val": 5.84
      },
      {
        "qty": 1500,
        "val": 7.66
      }
    ]
  }
]

let arr1Set =  new Set(arr1.map(o => o.number));
let newArry = arr1.concat(...arr2.filter(o => !arr1Set.has(o.number)))
console.log(newArry);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related