在数组中合并两个对象 [英] Merge two object in a array

查看:124
本文介绍了在数组中合并两个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个数组

  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

这篇关于在数组中合并两个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆