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

前端 未结 4 1224
逝去的感伤
逝去的感伤 2020-12-10 09:54

I have 2 arrays of objects, they each have an id in common. I need a property from objects of array 2 added to objects array 1, if they have matching id<

4条回答
  •  囚心锁ツ
    2020-12-10 10:34

    You can use pick to get only the properties you want before merging:

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

提交回复
热议问题