如何合并包含对象的两个数组

时间:2022-10-04 12:13:40

There is an array A:

有一个数组A:

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

And array B:

和阵列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.

我需要newArr中对象的活动属性等于或者arrA和arrB对象的活动属性之间或者名称相同。

arrA and arrB can have of different length

arrA和arrB可以有不同的长度

3 个解决方案

#1


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!

希望有所帮助!

#2


0  

  1. Create an empty array: var newArr = [];
  2. 创建一个空数组:var newArr = [];

  3. Push object into newArr where object key and value are compared between arrA and arrB.
  4. 将对象推入newArr,其中arrA和arrB之间的对象键和值进行比较。

  5. 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);

#3


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))];
}

#1


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!

希望有所帮助!

#2


0  

  1. Create an empty array: var newArr = [];
  2. 创建一个空数组:var newArr = [];

  3. Push object into newArr where object key and value are compared between arrA and arrB.
  4. 将对象推入newArr,其中arrA和arrB之间的对象键和值进行比较。

  5. 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);

#3


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))];
}