简体   繁体   English

如何对数组进行重复数据删除,并根据其重复向数组的每个 object 添加其他属性

[英]How can I de-dupe an array, and add additional properties to each object of the array based on its duplications

Let's say I have an array that looks like this:假设我有一个如下所示的数组:

[
  { id 1, date: "1/1/2021", count: 2, oldCount: 1 },
  { id 1, date: "2/1/2021", count: 3, oldCount: 2 },
  { id 2, date: "1/1/2021", count: 2, oldCount: 1 },
  { id 2, date: "2/1/2021", count: 3, oldCount: 2 }
]

The real key here is the combination of id and date .这里真正的关键是iddate的组合。 I want the final result to look like this:我希望最终结果如下所示:

[
  { id 1, "1/1/2021": 2, "2/1/2021": 3, "1/1/2021_old": 1, "2/1/2021_old": 2 },
  { id 2, "2/1/2021": 3, "2/1/2021": 3, "1/1/2021_old": 1, "2/1/2021_old": 2 }
]

I'd prefer to use lodash for this.我更喜欢为此使用lodash The part that hangs me up is the additional properties to add to each object in the array.让我感到困惑的部分是要添加到阵列中每个 object 的附加属性。 In our real world scenario, there could be several thousand records that need to be deduped and mapped, and this is resource intensive.在我们的现实世界场景中,可能有数千条记录需要进行重复数据删除和映射,这是资源密集型的。

You can see the logic.你可以看到其中的逻辑。 It's grouping by id , and adding a property name based on the value of the original date property.它按id分组,并根据原始date属性的值添加属性名称。

Here's a non-working example of my desired functionality:这是我想要的功能的一个无效示例:

arr = _.map(_.groupBy(data, "id"), objs => {              
  return _.assignWith({ /* I don't know the dates to set the property names */ }, ...objs)
});

I can do this with nested loops.我可以用嵌套循环来做到这一点。 I'm looking for best performance options and less lines of code.我正在寻找最佳性能选项和更少的代码行。

Group by id , map the groups, and then map each group.id 、 map 分组,然后是 map 每个组。 When mapping the objects of the group, create a new object with the [date] and [`${}_old`] keys with the respective values, and then use _.merge() with array spread to merge all objects to a single one.映射组的对象时,创建一个新的 object,其中[date][`${}_old`]键具有各自的值,然后使用带有数组扩展的_.merge()将所有对象合并为一个一。

 const arr = [{"id":1,"date":"1/1/2021","count":2,"oldCount":1},{"id":1,"date":"2/1/2021","count":3,"oldCount":2},{"id":2,"date":"1/1/2021","count":2,"oldCount":1},{"id":2,"date":"2/1/2021","count":3,"oldCount":2}] const result = _.map( _.groupBy(arr, 'id'), group => _.merge( {}, ..._.map(group, ({ id, date, count, oldCount }) => ({ id, [date]: count, [`${date}_old`]: oldCount })) ) ) console.log(result)
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ==" crossorigin="anonymous"></script>

And the same idea with lodash/fp, using _.flow() to generate a more readable function, and to merge all items ig a group using _.mergeAll() :与 lodash/fp 的想法相同,使用_.flow()生成更具可读性的 function,并使用_.mergeAll()合并所有项目 ig 一个组:

 const { flow, groupBy, map, mergeAll } = _ const fn = flow( groupBy('id'), map(flow( map(({ id, date, count, oldCount }) => ({ id, [date]: count, [`${date}_old`]: oldCount })), mergeAll )) ) const arr = [{"id":1,"date":"1/1/2021","count":2,"oldCount":1},{"id":1,"date":"2/1/2021","count":3,"oldCount":2},{"id":2,"date":"1/1/2021","count":2,"oldCount":1},{"id":2,"date":"2/1/2021","count":3,"oldCount":2}] const result = fn(arr) console.log(result)
 <script src='https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)'></script>

A simple Array.reduce should produce what you need -- no library or external dependencies necessary and still very brief.一个简单的Array.reduce应该会产生您需要的东西——不需要库或外部依赖项,而且仍然非常简短。

 const input = [ { id: 1, date: "1/1/2021", count: 2, oldCount: 1 }, { id: 1, date: "2/1/2021", count: 3, oldCount: 2 }, { id: 2, date: "1/1/2021", count: 2, oldCount: 1 }, { id: 2, date: "2/1/2021", count: 3, oldCount: 2 } ]; const output = Object.values(input.reduce((a, c) => { a[c.id] = a[c.id] || {id: c.id}; a[c.id][c.date] = c.count; a[c.id][`${c.date}_old`] = c.oldCount; return a; }, {})); console.log(output);

暂无
暂无

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

相关问题 删除数组中以前的相似条目 - de-dupe previous similar entries in an array 有没有一种方法可以对JavaScript数组进行重复数据删除并合并数据值? - Is there a way to de-dupe a javascript array and combine values of the data? 使用Array.prototype.filter()不会对数组重复数据删除 - Using Array.prototype.filter() doesn't de-dupe array of arrays 如何在 javascript 中将数组转换为 object 时添加其他属性? - How to add additional properties while converting array to object in javascript? 如何根据值将 object 添加到数组中? - How to add an object to an array based on its values? javascript 如何在不删除数组现有属性的情况下向循环内的数组坐标添加其他属性 - javascript how can I add additional properties to an array's coordinates inside of a loop, without erasing the existing properties of the array 如何为数组中的每个对象添加唯一 ID? - How can I add a unique ID to each object in an array? 如何根据每个 object 中的数组从 object 数组中返回某些对象? - How can I return certain objects from object array based on an array inside each object? 如何根据另一个数组对象的某些属性创建一个对象数组? - How can I create an array of objects based on some of another array object's properties? 如何动态地向javascript数组中的每个对象添加属性 - How to add properties dynamically to each object in an javascript array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM