在数组中合并两个对象

编程入门 行业动态 更新时间:2024-10-27 20:28:55
本文介绍了在数组中合并两个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有这个数组

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])里面 collat​​eBy 。此过程不了解您可能正在整理的数据的种类。

//通用归类procedureconst collat​​eBy = f => g => xs => {return xs.reduce((m,x)=> {let v = f(x)return m.set(v,g(m.get(v),x))},new Map())} / / reusable collat​​eById procedureconst collat​​eById = collat​​eBy(x => x.id)//自定义concatNames过程//注意使用`{name:[]}`这是一个空的collat​​ionconst的seed值concatNames = xs => ; {let collat​​ion = collat​​eById((a = {name:[]},b)=> Object.assign(a,b,{name:[... a.name,b.name]}))(xs)返回Array.from(collat​​ion.values())} // sample datalet arr1 = [{id:1,name:lorem},{id:1,name:ipsum},{id:2,name: dolor}] console.log(concatNames(arr1))

更高阶的函数演示了如何强大的通用程序喜欢 collat​​eBy 可以。这是另一个例子,使用完全相同的 collat​​eBy 程序,但执行非常不同的排序规则

const collat​​eBy = f => g => xs => {return xs.reduce((m,x)=> {let v = f(x)return m.set(v,g(m.get(v),x))},new Map())} const collat​​eEvenOdd = collat​​eBy(x => x%2 === 0?'even':'odd')const sumEvenOdd = collat​​eEvenOdd((a = 0,b)=> a + b)let data = [2,3 ,4,5,6,7] let collat​​ion = sumEvenOdd(data)let even = collat​​ion.get('even')let odd = collat​​ion.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

更多推荐

在数组中合并两个对象

本文发布于:2023-10-24 23:53:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1525352.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组中   对象   两个

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!