在数组中合并两个对象 [英] Merge two object in a array
问题描述
我有这个数组
var arr1 = [{id:1,name:lorem},{id: 1,name:ipsum},{id:2,name:dolor}]
正如你可以在这里看到前两个索引,他们得到相同的 id
,我想要输出为这样的东西
newArr
[
{
id:1,
名称:[lorem,ipsum]
},
{
id:2,
名称:dolor
}
]
我更喜欢这个解决方案,因为它抽象出了整理但是允许您使用高阶函数来控制项目的整理方式。
请注意,我们不会对 x.id
或 x.name
或 names.concat([name])
里面 collateBy
。此过程不了解您可能正在整理的数据的种类。
//通用归类procedureconst collateBy = f => g => xs => {return xs.reduce((m,x)=> {let v = f(x)return m.set(v,g(m.get(v),x))},new Map())} / / reusable collateById procedureconst collateById = collateBy(x => x.id)//自定义concatNames过程//注意使用`{name:[]}`这是一个空的collationconst的seed值concatNames = xs => ; {let collation = collateById((a = {name:[]},b)=> Object.assign(a,b,{name:[... a.name,b.name]}))(xs)返回Array.from(collation.values())} // sample datalet arr1 = [{id:1,name:lorem},{id:1,name:ipsum},{id:2,name: dolor}] console.log(concatNames(arr1))
更高阶的函数演示了如何强大的通用程序喜欢 collateBy
可以。这是另一个例子,使用完全相同的 collateBy
程序,但执行非常不同的排序规则
const collateBy = f => g => xs => {return xs.reduce((m,x)=> {let v = f(x)return m.set(v,g(m.get(v),x))},new Map())} const collateEvenOdd = collateBy(x => x%2 === 0?'even':'odd')const sumEvenOdd = collateEvenOdd((a = 0,b)=> a + b)let data = [2,3 ,4,5,6,7] let collation = sumEvenOdd(data)let even = collation.get('even')let odd = collation.get('odd')console.log('even sum',even)/ / 2 + 4 + 6 === 12console.log('odd sum',odd)// 3 + 5 + 7 === 15
/ div>
I have this array
var arr1 = [{id: 1, name: "lorem"}, {id: 1, name: "ipsum"}, {id: 2, name: "dolor"}]
as you can see here the first 2 indexs they got same id
, I want the ouput to be something like this
newArr
[
{
id: 1,
name: ["lorem", "ipsum"]
},
{
id: 2,
name: "dolor"
}
]
I like this solution better because it abstracts away the collation but allows you to control how items are collated using a higher-order function.
Notice how we don't say anything about x.id
or x.name
or names.concat([name])
inside collateBy
. This procedure has no knowledge of the kind of data you might be collating.
// generic collation procedure
const collateBy = f => g => xs => {
return xs.reduce((m,x) => {
let v = f(x)
return m.set(v, g(m.get(v), x))
}, new Map())
}
// reusable collateById procedure
const collateById = collateBy (x => x.id)
// custom concatNames procedure
// note use of `{name:[]}` which is the "seed" value for an empty collation
const concatNames = xs=> {
let collation = collateById ((a={name:[]}, b) =>
Object.assign(a, b, { name: [...a.name, b.name] })
) (xs)
return Array.from(collation.values())
}
// sample data
let arr1 = [
{id: 1, name: "lorem"},
{id: 1, name: "ipsum"},
{id: 2, name: "dolor"}
]
console.log(concatNames (arr1))
Higher order functions demonstrate how powerful generic procedures likes collateBy
can be. Here's another example using the exact same collateBy
procedure but performing a very different collation
const collateBy = f => g => xs => {
return xs.reduce((m,x) => {
let v = f(x)
return m.set(v, g(m.get(v), x))
}, new Map())
}
const collateEvenOdd = collateBy (x => x % 2 === 0 ? 'even' : 'odd')
const sumEvenOdd = collateEvenOdd ((a=0, b) => a + b)
let data = [2,3,4,5,6,7]
let collation = sumEvenOdd (data)
let even = collation.get('even')
let odd = collation.get('odd')
console.log('even sum', even) // 2 + 4 + 6 === 12
console.log('odd sum', odd) // 3 + 5 + 7 === 15
这篇关于在数组中合并两个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!