简体   繁体   English

"根据属性值 lodash 将对象数组中的属性合并到另一个对象中"

[英]Merge property from an array of objects into another based on property value lodash

I have 2 arrays of objects, they each have an id<\/code> in common.我有 2 个对象数组,它们每个都有一个共同的id<\/code> 。 I need a property from objects of array 2 added to objects array 1, if they have matching id<\/code> s.如果它们具有匹配的id<\/code> ,我需要将数组 2 的对象的属性添加到对象数组 1 中。

Array 1:阵列 1:

[
    {
        id: 1,
        name: tom,
        age: 24
    },
    {
        id: 2,
        name: tim,
        age: 25
    },
    {
        id: 3,
        name: jack,
        age: 24
    },

]

Lodash remains a highly useful bag of utilities, but with the advent of ES6 some of its use cases are less compelling. Lodash仍然是非常有用的实用程序包,但是随着ES6的出现,它的一些用例变得不那么吸引人了。

For each object (person) in the first array, find the object in the second array with matching ID (see function findPerson ). 对于第一个数组中的每个对象(人),请在第二个数组中找到具有匹配ID的对象(请参见函数findPerson )。 Then merge the two. 然后合并两者。

function update(array1, array2) {
  var findPerson = id => array2.find(person => person.id === id);

  array1.forEach(person => Object.assign(person, findPerson(person.id));
}

For non-ES6 environments, rewrite arrow functions using traditional syntax. 对于非ES6环境,请使用传统语法重写箭头功能。 If Array#find is not available, write your own or use some equivalent. 如果Array#find不可用,请自行编写或使用等效的方法。 For Object.assign , if you prefer use your own equivalent such as _.extend . 对于Object.assign ,如果您喜欢使用自己的等效项,例如_.extend

This will merge all properties from array2 into array1 . 这会将所有属性从array2合并到array1 To only merge eyeColour : 只合并eyeColour

function update(array1, array2) {
  var findPerson = id => array2.find(person => person.id === id);

  array1.forEach(person => {
    var person2 = findPerson(person.id));
    var {eyeColour} = person2;
    Object.assign(person, {eyeColour});
  });
}

Just noticed Paul answered while I was working on my answer but I'll add my very similar code anyway: 刚刚注意到Paul在我回答问题时回答了,但是无论如何我都会添加非常相似的代码:

var getEyeColour = function (el) { return _.pick(el, 'eyeColour'); }
var out = _.merge(arr1, _.map(arr2, getEyeColour));

DEMO 演示

You can use pick to get only the properties you want before merging: 您可以使用pick在合并前仅获取所需的属性:

var result = _.merge( arr1, _.map( arr2, function( obj ) {
    return _.pick( obj, 'id', 'eyeColour' );
}));

A solution in plain Javascript 普通Javascript解决方案

This is a more generic solution for merging two arrays which have different properties to union in one object with a common key and some properties to add. 这是一种更为通用的解决方案,用于合并两个具有不同属性的数组,以使用一个公共键在一个对象中合并并添加一些属性。

 var array1 = [{ id: 1, name: 'tom', age: 24 }, { id: 2, name: 'tim', age: 25 }, { id: 3, name: 'jack', age: 24 }, ], array2 = [{ id: 1, gender: 'male', eyeColour: 'blue', weight: 150 }, { id: 2, gender: 'male', eyeColour: 'green', weight: 175 }, { id: 3, gender: 'male', eyeColour: 'hazel', weight: 200 }, ]; function merge(a, b, id, keys) { var array = [], object = {}; function m(c) { if (!object[c[id]]) { object[c[id]] = {}; object[c[id]][id] = c[id]; array.push(object[c[id]]); } keys.forEach(function (k) { if (k in c) { object[c[id]][k] = c[k]; } }); } a.forEach(m); b.forEach(m); return array; } document.write('<pre>' + JSON.stringify(merge(array1, array2, 'id', ['name', 'age', 'eyeColour']), 0, 4) + '</pre>'); 

I was looking for the same, but I want to match Id before merging我一直在寻找相同的东西,但我想在合并之前匹配 Id

And in my case, second array may have different number of items, finally I came with this:在我的情况下,第二个数组可能有不同数量的项目,最后我得到了这个:

var out = arr1.map(x => {
    return  { ...x, eyeColour:  arr2.find(y => x.id === y.id)?.eyeColour }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM