0

There is an array A:

   let arrA = [{
      name: 'twitter',
      active: true
    }, {
      name: 'medium',
      active: false
    },
     {
      name: 'platinum',
      active: false
    }
  ];

And array B:

let arrB = [{
      name: 'twitter',
      active: false
    }, {
      name: 'medium',
      active: false
    }
  ];

How can I end up with an array that looks like this:

let newArr = [{
      name: 'twitter',
      active: true
    }, {
      name: 'medium',
      active: false
    },
    {
      name: 'platinum',
      active: false
    }
  ];

I need that active property of objects in newArr is equal to an or between active property of objects of arrA and arrB where the name is the same.

arrA and arrB can have of different length

theShadow89
  • 1,287
  • 18
  • 40
  • Possible duplicate of [How to merge two arrays in Javascript and de-duplicate items](https://stackoverflow.com/questions/1584370/how-to-merge-two-arrays-in-javascript-and-de-duplicate-items) – Murat Karagöz Sep 20 '17 at 15:23
  • @MuratK. I need to merge an array of objects not of strings. If you can provide me another answer related I will close this question – theShadow89 Sep 20 '17 at 15:40
  • Changed the tags to JavaScript as there's literally nothing specific to TypeScript in this question – Ryan Cavanaugh Sep 20 '17 at 15:49

3 Answers3

2

That is my approach. Using map

const arrA = [{
    name: 'twitter',
    active: true
}, {
    name: 'medium',
    active: false
},{
    name: 'platinum',
    active: false
}];

const arrB = [{
    name: 'twitter',
    active: false
}, {
    name: 'medium',
    active: false
}];

  const allItems = arrA.concat(arrB);
  const mergedItems = new Map();
  allItems.forEach((value) => {
    if (!mergedItems.get(value.name)) {
      mergedItems.set(value.name, value);
    } else {
      value.active = value.active || mergedItems.get(value.name).active;
      mergedItems.set(value.name, value);
    }
  });
  const mergedArray =  Array.from(mergedItems.values()) ;

Hope that helps!

kimy82
  • 2,566
  • 18
  • 22
0
  1. Create an empty array: var newArr = [];
  2. Push object into newArr where object key and value are compared between arrA and arrB.
  3. Done.

let arrA = [
      {
        name: 'twitter',
        active: true
      },
      {
        name: 'medium',
        active: false
      }
    ];
let arrB = [
      {
        name: 'twitter',
        active: false
      },
      {
        name: 'medium',
        active: false
      }
    ];
let newArr = [];
    
for (var i = 0; i < arrA.length; i++){
  newArr.push({
    name: arrA[i].name,
    active: arrA[i].active || arrB[i].active,
  });
}

console.log(newArr);
yqlim
  • 5,648
  • 3
  • 15
  • 36
0

Here is a function for that purpose:

function MyFunction(firstArray, secondArray) {
    firstArray = firstArray.map(e => {
        if (secondArray.some(_e => _e.name === e.name))
            e.active = e.active || secondArray.filter(f => f.name == e.name)[0].active;
            return e;
        });

        return [...newA, ...arrB.filter(e => !arrA.some(s => s.name === e.name))];
}
Rui Silva
  • 384
  • 1
  • 3
  • 11