本文介绍了如何使用Lodash基于键合并两个集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个集合,对象有一个公共键userId。如下所示:

  var _ = require('lodash'); 

var a = [
{userId:p1,item:1},
{userId:p2,item:2},
{userId :p3,item:4}
];

var b = [
{userId:p1,profile:1},
{userId:p2,profile:2}
];

我想根据userId合并它们,产生:

  [{userId:'p1',item:1,profile:1},
{userId:'p2',item:2, 2},
{userId:'p3',item:4}]



这些到目前为止:

  var u = _.uniq(_。union(a,b),false,_.property '用户名')); 

其结果是:

  [{userId:'p1',item:1},
{userId:'p2',item:2},
{userId:'p3',item:4 },
{userId:'p1',profile:1},
{userId:'p2',profile:2}]



我尝试了_.keyBy,但结果是:

  {p1:{userId:'p1',profile:1},
p2:{userId:'p2',profile:2 },
p3:{userId:'p3',item:4}}

是错的。



我应该做的最后一步是什么?

解决方案

您可以使用,和。

  //迭代第一个对象数组
_.map(a,function(obj){

//将来自第二个数组的属性添加到userID
//到第一个数组的对象,并返回更新的对象
return _.assign(obj,_.find(b,{userId:obj.userId}));
});



  var a = [{userId: p1,item:1},{userId:p2,item:2},{userId:p3,item:4}]; var b = [{userId:p1,profile:1} userId:p2,profile:2}]; var arrResult = _.map(a,function(obj){return _.assign(obj,_.find(b,{userId:obj.userId})) ); console.log(arrResult); document.getElementById('result')。innerHTML = JSON.stringify(arrResult,0,4);   < script src =https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.1.0/lodash.min .js>< / script>< pre id =result>< / pre>  

>

I have two collections, and the objects have a common key "userId". As below:

var _= require('lodash');

var a = [
  { userId:"p1", item:1},
  { userId:"p2", item:2},
  { userId:"p3", item:4}
];

var b = [
  { userId:"p1", profile:1},
  { userId:"p2", profile:2}
];

I want to merge them based on "userId" to produce:

[ { userId: 'p1', item: 1, profile: 1 },
  { userId: 'p2', item: 2, profile:2 },
  { userId: 'p3', item: 4 } ]

I have these so far:

var u = _.uniq(_.union(a, b), false, _.property('userId'));

Which result in:

[ { userId: 'p1', item: 1 },
  { userId: 'p2', item: 2 },
  { userId: 'p3', item: 4 },
  { userId: 'p1', profile: 1 },
  { userId: 'p2', profile: 2 } ]

How can I merge them now?

I tried _.keyBy but it results in:

{ p1: { userId: 'p1', profile: 1 },
  p2: { userId: 'p2', profile: 2 },
  p3: { userId: 'p3', item: 4 } }

which is wrong.

What's the last step I should do?

解决方案

You can use _.map(), _.assign() and _.find().

// Iterate over first array of objects
_.map(a, function(obj) {

    // add the properties from second array matching the userID
    // to the object from first array and return the updated object
    return _.assign(obj, _.find(b, {userId: obj.userId}));
});

Fiddle Demo

var a = [{
    userId: "p1",
    item: 1
}, {
    userId: "p2",
    item: 2
}, {
    userId: "p3",
    item: 4
}];

var b = [{
    userId: "p1",
    profile: 1
}, {
    userId: "p2",
    profile: 2
}];

var arrResult = _.map(a, function(obj) {
    return _.assign(obj, _.find(b, {
        userId: obj.userId
    }));
});

console.log(arrResult);
document.getElementById('result').innerHTML = JSON.stringify(arrResult, 0, 4);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.1.0/lodash.min.js"></script>
<pre id="result"></pre>

这篇关于如何使用Lodash基于键合并两个集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-10 12:35