For each key match, how to filter individual list of array objects

Viewed 49

How to filter Hardware array objects by matching id with location, for each key match need to form new array

input

  export const Location = [
 {
   id: '37a8ab5c-6d5e-4733-850d-393cb57bd87a',
   Name: 'Cypress-Kolkata-A.J.c.bose road',
 },
 {
   id: '37a8ab5c-6d5e-4733-850d-393cb57bd87b',
   Name: 'Cypress-Bengal',
 },
 {
   id: '37a8ab5c-6d5e-4733-850d-393cb57bd87d',
   Name: 'Cypress-Bengall',
 },
];

export const Hardware = [
 {
   id: '37a8ab5c-6d5e-4733-850d-393cb57bd87a',
   Name: 'Cypress-Kolkata-A.J.c.bose road-H1',
 },
 {
   id: '37a8ab5c-6d5e-4733-850d-393cb57bd87a',
   Name: 'Cypress-Kolkata-A.J.c.bose road-H2',
 },
 {
   id: '37a8ab5c-6d5e-4733-850d-393cb57bd87b',
   Name: 'Cypress-Bengal-H1',
 },
 {
   id: '37a8ab5c-6d5e-4733-850d-393cb57bd87e',
   Name: 'Cypress-Bengal-H1',
 },
];

if location and hardware 'id' matches needs to store hardware objects in new array without modifying original array

expected output

result_hardware = [  {
    id: '37a8ab5c-6d5e-4733-850d-393cb57bd87a',
    Name: 'Cypress-Kolkata-A.J.c.bose road-H1',
  },
  {
    id: '37a8ab5c-6d5e-4733-850d-393cb57bd87a',
    Name: 'Cypress-Kolkata-A.J.c.bose road-H2',
  },
]

result_hardware = [ {
    id: '37a8ab5c-6d5e-4733-850d-393cb57bd87b',
    Name: 'Cypress-Bengal-H1',
  },]

I tried this , but all matching ids getting stored in single array

let output = devExpIncWIHardware.filter((hardware) =>
  devExpIncWILoc.some((loc) => hardware.id === loc.id)
);

console.log(output);

output

[
  {
    id: '37a8ab5c-6d5e-4733-850d-393cb57bd87a',
    Name: 'Cypress-Kolkata-A.J.c.bose road-H1',
  },
  {
    id: '37a8ab5c-6d5e-4733-850d-393cb57bd87a',
    Name: 'Cypress-Kolkata-A.J.c.bose road-H2',
  },
  {
    id: '37a8ab5c-6d5e-4733-850d-393cb57bd87b',
    Name: 'Cypress-Bengal-H1',
  }
]```
0 Answers
Related