Eliane Bheaver
Eliane Bheaver

Reputation: 81

Merge two array of object if a value is matching

I'm trying to create a new array of object with the merged value if some values end up to be equal.

For exemple:

const arr1 = [{name: "Sven", firstName: "Alan", age: 24, profile: "Hello, i'm Alan"}, {name: "Mign", firstName: "Alex", age: 44, profile: "Hello, i'm Alex"}];

const arr2 = [{location: "Miami", alias: "ALAN", isAlive: true}, {location: "Los Angeles", alias: "Ethic", isAlive: true}]

In that case the firstName of the first array is matching with the alias of the second array, in that case i would like to merge those to object together and return them like so:

const newArray = [{name: "Sven", firstName: "Alan", age: 24, profile: "Hello, i'm Alan",  isAlive: true, location: "Miami"}]

I have tried multiple solution but the closest i could get to make it work is:

  const newArray = arr1.map((item, i) => {
    const mapping = arr2.map(sym => sym.firstName.toLowerCase());
    if (mapping.includes(item.alias.toLowerCase())) {
      return Object.assign({}, item, arr2[i]);
    }
  });

In that case i'm merging with the index in the object.assign so my solution is not correct, everything will be mixed.

Upvotes: 8

Views: 2971

Answers (4)

Jaeden
Jaeden

Reputation: 342

Here is a fairly inefficient but very clear solution.

const arr1 = [{name: "Sven", firstName: "Alan", age: 24, profile: "Hello, i'm Alan"}, {name: "Mign", firstName: "Alex", age: 44, profile: "Hello, i'm Alex"}];
const arr2 = [{location: "Miami", alias: "ALAN", isAlive: true}, {location: "Los Angeles", alias: "Ethic", isAlive: true}];

arr3 = [];
for (const d1 of arr1) {
  for (const d2 of arr2) {
    if (d1.firstName.toLowerCase() == d2.alias.toLowerCase()) {
      arr3.push(Object.assign({}, d1, d2));
    }
  }
}

console.log(arr3);

Upvotes: 0

Nitheesh
Nitheesh

Reputation: 19986

Try using Array.reduce in javascript.

const arr1 = [
  { name: "Sven", firstName: "Alan", age: 24, profile: "Hello, i'm Alan" },
  { name: "Mign", firstName: "Alex", age: 44, profile: "Hello, i'm Alex" }
];

const arr2 = [
  { location: "Miami", alias: "ALAN", isAlive: true },
  { location: "Los Angeles", alias: "Ethic", isAlive: true }
];

const newArray = arr1.reduce((acc, curr) => {
  const searchNode = arr2.find((node) => node.alias.toLowerCase() === curr.firstName.toLowerCase());
  const newNode = searchNode ? { ...searchNode, ...curr } : null;
  if (newNode) {
    acc.push(newNode)
  }
  return acc;
}, []);

console.log(newArray);

Upvotes: 0

Amila Senadheera
Amila Senadheera

Reputation: 13245

Try this:

const arr1 = [{name: "Sven", firstName: "Alan", age: 24, profile: "Hello, i'm Alan"}, {name: "Mign", firstName: "Alex", age: 44, profile: "Hello, i'm Alex"}];
const arr2 = [{location: "Miami", alias: "ALAN", isAlive: true}, {location: "Los Angeles", alias: "Ethic", isAlive: true}]

output = [];

arr1.forEach(item => {
const match = arr2.find(
    item2 => item.firstName.toLowerCase() === item2.alias.toLowerCase()
);
if (match) {
    output.push({ ...item, ...match });
}
});

console.log(output);

Upvotes: 2

nivendha
nivendha

Reputation: 837

Use array spread to achive merging

const arr1 = [{name: "Sven", firstName: "Alan", age: 24, profile: "Hello, i'm Alan"}, {name: "Mign", firstName: "Alex", age: 44, profile: "Hello, i'm Alex"}];

const arr2 = [{location: "Miami", alias: "ALAN", isAlive: true}, {location: "Los Angeles", alias: "Ethic", isAlive: true}]

const mergedData = arr1.map(data=>({
...data,
...arr2.find(newData=>newData.alias.toLowerCase() == data.firstName.toLowerCase())
}))

console.log(mergedData)

Upvotes: 4

Related Questions