根据Javascript中的公共值合并同一数组中的对象

手指他们

我有一个数组:

[
  {
    assignmentId:17,
    email:"[email protected]"
    expectation: "Make sure to proofread!",
    firstName:"John"
    id:23
    ignoreForFeedback: true
    lastName:"Smith"
    level:2
    levelFraction:null
    score:35
  },
  {
    assignmentId:17
    countsPerCategory: Array(4)
    email:"[email protected]"
    firstName:"John"
    frequentErrors: Array(5)
    id:23
    ignoreForGrading: true
    lastName:"Smith"
  },
  {
    assignmentId:17,
    email:"[email protected]"
    expectation: "cite sources",
    firstName:"Cindy"
    id:45
    ignoreForFeedback: true
    lastName:"Lee"
    level:2
    levelFraction:null
    score:32
  },
  {
    assignmentId:17
    countsPerCategory: Array(4)
    email:"[email protected]"
    firstName:"Cindy"
    frequentErrors: Array(5)
    id:45
    ignoreForGrading: true
    lastName:"Lee"
  }
]

我想将具有相同“id”的对象组合到数组中的同一个对象中。它们的公共键也应该组合在一起(例如:'firstName'、'email')。有人可以建议最好的方法吗?使用 ES6 或 Lodash

黑麦巴拉

您可以使用lodash#groupByby 对数组中的所有项目进行分组id,然后lodash#maplodash#assign用 a 包装的迭代对象一起使用,lodash#spread以使数组回调作为 的参数列表lodash#assgin

var result = _(array)
  .groupBy('id')
  .map(_.spread(_.assign))
  .value();

var array = [
  {
    assignmentId:17,
    email:"[email protected]",
    expectation: "Make sure to proofread!",
    firstName:"John",
    id:23,
    ignoreForFeedback: true,
    lastName:"Smith",
    level:2,
    levelFraction:null,
    score:35
  },
  {
    assignmentId:17,
    countsPerCategory: Array(4),
    email:"[email protected]",
    firstName:"John",
    frequentErrors: Array(5),
    id:23,
    ignoreForGrading: true,
    lastName:"Smith"
  },
  {
    assignmentId:17,
    email:"[email protected]",
    expectation: "cite sources",
    firstName:"Cindy",
    id:45,
    ignoreForFeedback: true,
    lastName:"Lee",
    level:2,
    levelFraction:null,
    score:32
  },
  {
    assignmentId:17,
    countsPerCategory: Array(4),
    email:"[email protected]",
    firstName:"Cindy",
    frequentErrors: Array(5),
    id:45,
    ignoreForGrading: true,
    lastName:"Lee"
  }
];

var result = _(array)
  .groupBy('id')
  .map(_.spread(_.assign))
  .value();
  
console.log(result);
body > div { min-height: 100%; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>

这是一个替代解决方案,Array#filter它利用了 的第二个参数,该参数为Array#filter过滤器的回调函数提供了上下文。我们使用this上下文作为一种机制来存储缓存的对象id,然后使用它来决定是否从数组中保留这些对象。

var result = array.filter(function(v) {
  return this[v.id]?
    !Object.assign(this[v.id], v):
    (this[v.id] = v);
}, {});

var array = [
  {
    assignmentId:17,
    email:"[email protected]",
    expectation: "Make sure to proofread!",
    firstName:"John",
    id:23,
    ignoreForFeedback: true,
    lastName:"Smith",
    level:2,
    levelFraction:null,
    score:35
  },
  {
    assignmentId:17,
    countsPerCategory: Array(4),
    email:"[email protected]",
    firstName:"John",
    frequentErrors: Array(5),
    id:23,
    ignoreForGrading: true,
    lastName:"Smith"
  },
  {
    assignmentId:17,
    email:"[email protected]",
    expectation: "cite sources",
    firstName:"Cindy",
    id:45,
    ignoreForFeedback: true,
    lastName:"Lee",
    level:2,
    levelFraction:null,
    score:32
  },
  {
    assignmentId:17,
    countsPerCategory: Array(4),
    email:"[email protected]",
    firstName:"Cindy",
    frequentErrors: Array(5),
    id:45,
    ignoreForGrading: true,
    lastName:"Lee"
  }
];

var result = array.filter(function(v) {

  // does this `id` exist?
  return this[v.id]? 
  
    // assign existing object with the same id
    // from the `this` cache object. Make sure
    // to negate the resulting object with a `!`
    // to remove this value from the array
    !Object.assign(this[v.id], v):
    
    // Assign the value from the `this` cache.
    // This also retains this value from the existing
    // array
    (this[v.id] = v);
    
}, {});

console.log(result);
body > div { min-height: 100%; top: 0; }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

根据通用值合并同一数据框中的行

在单个键上合并同一数组中的对象

通过两个具有不同键的键合并同一数组中的对象

PHP通过同一数组中的值合并

如何合并同一数据框的多行?或者如何在python中合并同一个键对的多个值?

比较同一数组中的对象JavaScript

合并同一个数组中的值(Python)?

合并键在同一数组中匹配的关联数组值

如果它们的索引相同,则合并同一数据框中的两行吗?

如何在共享相同未知密钥的同一数组中合并多个对象?

通过相等属性合并对象,并在同一数组中通过不同属性合并对象

将对象值与同一数组中的其他对象进行比较

合并熊猫中同一数据框内的列值

将具有相同值的数组中的对象排序到同一数组中的一个对象

Javascript根据特定值合并数组中的对象

如何根据名称合并数组javascript中的重复对象值

在 JavaScript 中连接来自同一数组的数组

在Ruby的同一数组中串联现有值

如何将两个值合并/组合到同一数组中的单个键中

如果两对具有相同的值,则将它们合并在同一数组中

合并同一表中的列,并根据ID显示两列的总和

将数组合并到同一数组中的不同集合中

更改同一数组中的另一个对象时,正在更改的Javascript对象

合并同一数据框内的列

使用 R 合并同一数据集中的列

Javascript - 根据公共值重构对象数组

如何在java中添加和合并同一地图的值

Pandas 数据帧根据条件从同一数据帧中的两列中减去值?

熊猫,仅合并同一组中的行