简体   繁体   English

合并两个JSON对象数组的JSON属性(使用ramda)

[英]Merging JSON properties for two arrays of JSON objects (using ramda)

Consider that I have two arrays as follows: 考虑一下我有两个数组,如下所示:

let a1 = [{id:1, name:'jon'}, {id:2, name:'adam'}]
let a2 = [{id:1, age:42}, {id:2, age:13}]

I want to merge the two arrays so the JSON properties get combined into one array; 我想合并两个数组,以便将JSON属性合并为一个数组; ie this is what I want to end up with: 即这就是我最终要得到的结果:

[{ id: 1, name: 'jon', age: 42 },{ id: 2, name: 'adam', age: 13 }]

Using Ramda I can do the following which gives me close to what I want, but annoyingly it's not in an array - it's all JSON 使用Ramda我可以执行以下操作,使我接近所需的内容,但令人讨厌的是它不在数组中-都是JSON

R.mergeDeepLeft(a1, a2)
{ '0': { id: 1, name: 'yasin', age: 42 },
  '1': { id: 2, name: 'adam', age: 13 } }

I'd do this: 我会这样做:

  1. concat a1 and a2 together. 同时连接a1a2 (They don't need to be kept apart) (它们不必分开)
  2. group objects by id and merge those that share the same id. 按ID对对象进行分组,并合并共享相同ID的对象。
  3. finally get all merged objects out. 最终取出所有合并的对象。

 const res = pipe( concat, reduceBy(mergeLeft, {}, prop('id')), values); console.log( res(a1, a2) ); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.min.js"></script> <script> const {pipe, concat, reduceBy, mergeLeft, values, prop} = R; const a1 = [{id:1, name:'jon'}, {id:2, name:'adam'}]; const a2 = [{id:1, age:42}, {id:2, age:13}]; </script> 

Perhaps not the most elegant nor extensible solution but I'd use some mapping and the spread operator: 也许不是最优雅也不可扩展的解决方案,但我会使用一些映射和散布运算符:

let a1 = [{id:1, name:'jon'}, {id:2, name:'adam'}]
let a2 = [{id:1, age:42}, {id:2, age:13}]
let a3 = a1.map(item => {
    return {...item, ...a2.find(item2 => item2.id === item.id)}
})

Here's a working Fiddle for it https://jsfiddle.net/gshfo7pm/ 这是工作的小提琴https://jsfiddle.net/gshfo7pm/

Try this: 尝试这个:

 let a1 = [{id:1, name:'jon'}, {id:2, name:'adam'}] let a2 = [{age:13, id:2}, {id:1, age:42}] const fn = R.pipe( R.map(R.indexBy(R.prop('id'))), R.reduce(R.mergeWith(R.mergeDeepLeft), {}), R.values ) let newArray1 = fn([a1, a2]); let newArray2 = fn([a2, a1]); console.log(newArray1); console.log(newArray2); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script> 

You can use R.values to convert to array, like this: 您可以使用R.values转换为数组,如下所示:

let a1 = [{id:1, name:'jon'}, {id:2, name:'adam'}]
let a2 = [{id:1, age:42}, {id:2, age:13}]

R.values(R.mergeWith(R.mergeLeft, a1, a2));

If you have unordered arrays, you can do this: 如果您有无序数组,则可以执行以下操作:

let a1 = [{id:1, name:'jon'}, {id:2, name:'adam'}]
let a2 = [{id:2, age:13}, {id:1, age:42}]

R.values(R.mergeWith(
  R.mergeLeft, 
  R.indexBy(R.prop('id'), a1), 
  R.indexBy(R.prop('id'), a2)
));
let concatValues = (k, l, r) =>  r
R.mergeDeepWithKey(concatValues,a1,a2)

will produce this 会产生这个

{"0": {"age": 42, "id": 1, "name": "jon"}, "1": {"age": 13, "id": 2, "name": "adam"}}

it's not exact output but you can start with this 它不是确切的输出,但是您可以从此开始

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

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