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 =, i) => {
    const mapping = => 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.

Read more here:

Content Attribution

This content was originally published by Eliane Bheaver at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: