Merge Two Json Array on Condition in lodash

时间:2018-06-05 05:02:29

标签: javascript ecmascript-6 lodash

I have Two Json Array Like

var a = [{_id:1, name: "Bhavin"},{_id:2, name: "Raj"},{_id:3, name: "Rahul"}];    
var b = [{_id:1, post: "Developer"},{_id:2, post: "Quality Analyst"}];

Now, I want merged Like :

var c = [{_id:1, name: "Bhavin", post: "Developer"},{_id:2, name: "Raj", post: "Quality Analyst"},{_id:3, name: "Rahul"}];

I know I can do it easily in Plain Javascript by the use of two for loop... But that takes n*n time.

I wan to solve this problem in only n time.

How can I achieve that ?

4 个答案:

答案 0 :(得分:1)

您应该使用lodash mergeWith功能。



var a = [{_id:1, name: "Bhavin"},{_id:2, name: "Raj"},{_id:3, name: "Rahul"}];

var b = [{_id:1, post: "Developer"},{_id:2, post: "Quality Analyst"}];


// ouput [{_id:1, name: "Bhavin", post: "Developer"},{_id:2, name: "Raj", post: "Quality Analyst"},{_id:3, name: "Rahul"}];

function customizer(firstValue, secondValue) {
  return Object.assign({}, firstValue, secondValue);
}

console.log(_.mergeWith(a, b, customizer));

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.js"></script>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您可以通过将较小尺寸的数组映射到键值组合对象来轻松解决此问题,其中对象的键是id,它们的值是关联值本身。完成关联后,我们可以通过从映射对象中分配缺失值来迭代和转换更大的数组。

// ensure that b has more items than a
// to prevent data loss
if(a.length > b.length) {
  var t = b;
  b = a;
  a = t;
}

// create an object composed of of ids as key associated with each object.
// a loash alternative is: 
// var map = _.keys(a, '_id');
var map = a.reduce((r, v) => (r[v._id] = v, r), {});

// assign missing properties from each object from the associated mapped object.
var result = b.map(v => ({...map[v._id], ...v}));

var a = [{_id:1, name: "Bhavin"},{_id:2, name: "Raj"},{_id:3, name: "Rahul"}];
var b = [{_id:1, post: "Developer"},{_id:2, post: "Quality Analyst"}];

// ensure that b has more items than a
// to prevent data loss
if(a.length > b.length) {
  var t = b;
  b = a;
  a = t;
}

// create an object composed of of ids as key associated with each object.
// a loash alternative is: 
// var map = _.keys(a, '_id');
var map = a.reduce((r, v) => (r[v._id] = v, r), {});

// assign missing properties from each object from the associated mapped object.
var result = b.map(v => ({...map[v._id], ...v}));

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

答案 2 :(得分:0)

It's quite possible to achieve this in plain Javascript in O(n) time:

var a = [{_id:1, name: "Bhavin"},{_id:2, name: "Raj"},{_id:3, name: "Rahul"}];
var b = [{_id:1, post: "Developer"},{_id:2, post: "Quality Analyst"}];

const aById = a.reduce((map, { _id, name }) => map.set(_id, name), new Map());
const bById = b.reduce((map, { _id, post }) => map.set(_id, post), new Map());
const combined = [...aById.entries()].map(([_id, name]) => {
  const person = { _id, name };
  const post = bById.get(_id);
  if (post) person.post = post;
  return person;
});

console.log(combined);
var c = [{_id:1, name: "Bhavin", post: "Developer"},{_id:2, name: "Raj", post: "Quality Analyst"},{_id:3, name: "Rahul"}];

Maps are guaranteed to have O(1) lookup time. Object hashmaps (like {'1': 'Developer', '2': 'Quality Analyst'}) are not.

You had a couple of typos in your original code, I'm assuming those were unintentional - I fixed them in the snippet above.

答案 3 :(得分:0)

您好,可以使用以下代码

来实现
_.map(a, function (el){
   return _.merge(el , _.find(b, {_id: el._id }))
})

// using chain, i would prefer first it looks clean :-p
_.chain(a).map(function (o){
   return _.merge(o, _.find(b, {_id: o._id }))
})
.value()

我会尝试使用链并更新答案