简体   繁体   English

如何在Javascript中按键合并数组和分组?

[英]How can I merge an array and group by key in Javascript?

I have multiple array in reactjs.我在 reactjs 中有多个数组。

{last_name: 'User1', status: 'BRONZE', type: 'Maintenance', due_date: '2022-06-04 00:00:00'}
{last_name: 'User1', status: 'BRONZE', type: 'Contrôle technique', due_date: '2022-06-18 00:00:00'}
{last_name: 'User2', status: 'BRONZE', type: 'Unknow', due_date: null}

I would like to merge the array by user last_name to have a result like this:我想通过用户 last_name 合并数组以获得如下结果:

{last_name: 'User1', status1: 'BRONZE', type: 'Maintenance1', due_date1: '2022-06-04 00:00:00', status2: 'BRONZE', type2: 'Contrôle technique', due_date: '2022-06-18 00:00:00'}
{last_name: 'User2', status: 'BRONZE', type: 'Unknow', due_date: null}

In my example I have merge the array 1 and 2 to have 1 array "group by" last_name, here User1 but I need to keep the value of the second array too.在我的示例中,我将数组 1 和 2 合并为 1 个数组“group by”last_name,这里是 User1,但我也需要保留第二个数组的值。

Your question lacks a few details.你的问题缺少一些细节。 Are status and due_date the only fields that might be repeated? statusdue_date是唯一可能重复的字段吗? If so, the below answer should work.如果是这样,下面的答案应该有效。 If not, you might want to specify which keys should be merged with an index in the field name, and which can be pulled in as is.如果没有,您可能希望指定哪些键应该与字段名称中的索引合并,哪些可以按原样拉入。

I'm not sure why you would want to structure your data this way-- having different field names seems like it would make it difficult to find the data, but leaving that aside:我不确定您为什么要以这种方式构建数据 - 使用不同的字段名称似乎会使查找数据变得困难,但将其搁置一旁:

 const data = [{ last_name: 'User1', status: 'BRONZE', type: 'Maintenance', due_date: '2022-06-04 00:00:00' }, { last_name: 'User1', status: 'BRONZE', type: 'Contrôle technique', due_date: '2022-06-18 00:00:00' }, { last_name: 'User2', status: 'BRONZE', type: 'Unknow', due_date: null }] const mergedMap = {} // Group list elements by last_name for (const el of data) { if (el.last_name in mergedMap) { mergedMap[el.last_name].push(el) } else { mergedMap[el.last_name] = [el] } } // Iterate over "user" groups, modifying field names. const mergedList = [] for (const last_name in mergedMap) { const elCount = mergedMap[last_name].length // If there's only one entry for this "last_name", keep it as is, // then continue to next user. if (elCount === 1){ mergedList.push(mergedMap[last_name][0]) continue } const mergedUser = mergedMap[last_name].reduce((merged, el, index) => ({ // Keep whatever keys are already here ...merged, // last_name and status are assumed to always be the same // for a given user, so they're safe to overwrite each time last_name: el.last_name, status: el.status, // type and due_date might be unique for each entry, so // we add an index to the field name and copy the new value in [`type${index + 1}`]: el.type, [`due_date${index + 1}`]: el.due_date, }), {}) mergedList.push(mergedUser) } console.log(JSON.stringify(mergedList, null, 2))

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM