如何比较 2 个数组的对象条目的相同性以及如何使用找到的相同对象的自定义属性创建两个对象的合并?

How to compare the sameness of object entries of 2 arrays and how to create a merger of both objects with custom properties of the found same object?

我正在尝试通过 属性 和值 严格使用 forloop 来比较 2 个对象。如果“名称”或另一个 属性 的值彼此匹配,我想将 属性 和值推送到 value3.

记录 value3 后,我想要这样的响应:

{
name: 'dog',
surname: 'good',
skills: 'programming',
age: '22'
},
{
name: 'cat',
surname: 'soft',
skills: 'engineer'
age: '12'
},
{
name: 'elephant',
surname: 'big',
skills: 'programming'
age: '23'
}

代码如下:

var values1 = [
    {
    name: 'dog',
    surname: 'good',
    skills: 'programming'
    },
    {
    name: 'cat',
    surname: 'soft',
    skills: 'engineer'
    },
    {
    name: 'elephant',
    surname: 'big',
    skills: 'programming'
    }
]

var values2 = [
    {
    name: 'cat',
    food: 'fish',
    age: '12'
    },
    {
    name: 'elephant',
    food: 'leafs',
    age: '13'
    },
    {
    lastname: 'dog',
    food: 'treats',
    age: '22'
    }
]

// push into this empty object array
var values3 = [{}]

console.log(values3)

如果你只想要键值对,你可以这样做:

var values1 = [
    {
    name: 'dog',
    surname: 'good',
    skills: 'programming'
    },
    {
    name: 'cat',
    surname: 'soft',
    skills: 'engineer'
    },
    {
    name: 'elephant',
    surname: 'big',
    skills: 'programming'
    }
]

var values2 = [
    {
    name: 'cat',
    food: 'fish',
    age: '12'
    },
    {
    name: 'elephant',
    food: 'leafs',
    age: '13'
    },
    {
    lastname: 'dog',
    food: 'treats',
    age: '22'
    }
]

// push into this empty object array
var values3 = [];

values1.forEach(eachValue1Obj => {
  const keys = Object.keys(eachValue1Obj);
  keys.forEach(eachKey => {
    values2.forEach(eachValue2Obj => {
      if (
        eachValue1Obj[eachKey] &&
        eachValue2Obj[eachKey] &&
        eachValue1Obj[eachKey] === eachValue2Obj[eachKey]
      ) {
        const x = {
          key: eachKey,
          value: eachValue1Obj[eachKey]
        };
        values3.push(x)
      }
    })
  })
})

console.log('Values 3 Array ==>', values3);

满足 OP 的所有要求的最通用的方法应该基于 Array.prototype.reduce. Its advantage comes with utilizing the additionally passed optional initial value 作为一种可配置的 collector/accumulator 对象,它将携带所有需要的附加信息/功能/结果。因此,可以提供一个可重用的功能和可定制的 context/environment,可以适应自己的需要。

var values1 = [{
  name: 'dog',
  surname: 'good',
  skills: 'programming',
}, {
  name: 'cat',
  surname: 'soft',
  skills: 'engineer',
}, {
  name: 'elephant',
  surname: 'big',
  skills: 'programming',
}];

var values2 = [{
  name: 'cat',
  food: 'fish',
  age: '12'
}, {
  name: 'elephant',
  food: 'leafs',
  age: '13'
}, {
  lastname: 'dog',
  food: 'treats',
  age: '22'
}];

function mergeItemsOfSameEntry(collector, item) {
  const {
    getSameEntryValue,
    getMergeSubType,
    comparisonItems,
    result
  } = collector;

  const itemValue = getSameEntryValue(item);
  const comparisonItem = comparisonItems
    .find(cItem => getSameEntryValue(cItem) === itemValue);

  if (comparisonItem !== null) {
    result.push({
      ...item,
      ...getMergeSubType(comparisonItem),
    });
  }
  return collector;
}

const values3 = values1.reduce(mergeItemsOfSameEntry, {

  getSameEntryValue: item => item.name ?? item.lastname,
  getMergeSubType: ({ age }) => ({ age }),
  comparisonItems: values2,
  result: [],

}).result;

console.log({ values3 });
.as-console-wrapper { min-height: 100%!important; top: 0; }