繁体   English   中英

合并对象数组中的对象删除重复项

[英]Merge objects in array of object removing duplicates

我有这个对象数组:

const a = [
   {
      id: 1,
      name: 'John',
      role: 'admin'
   },
   {
      id: 1,
      name: 'John',
      role: 'user'
   },
   {
      id: 2,
      name: 'Max',
      role: 'user'
   }  
]

我想得到这样的结果,所以在角色属性中有一个id:1对象和一个合并数组:

const a = [
   {
      id: 1,
      name: 'John',
      role: ['admin', 'user']
   },
   {
      id: 2,
      name: 'Max',
      role: 'user'
   }  
]

编辑:当我只有对象中的属性时,我可以删除重复项。 就我而言,我不知道如何使用以下代码段检索name属性:

const b = [...new Set(a.map(d => d.id))].map(obj => {
  return {
    id: obj,
    data: a.filter(d => d.id === obj).map(d => d.role)
  }
})

您可以使用一个对象进行分组,并将一个数组用于其他角色。

 const data = [{ id: 1, name: 'John', role: 'admin' }, { id: 1, name: 'John', role: 'user' }, { id: 2, name: 'Max', role: 'user' }], result = Object.values(data.reduce((r, o) => { if (!r[o.id]) r[o.id] = { ...o }; else r[o.id].role = [].concat(r[o.id].role, o.role); return r; }, {})); console.log(result);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

试试这个

 const a = [ { id: 1, name: 'John', role: 'admin' }, { id: 1, name: 'John', role: 'user' }, { id: 2, name: 'Max', role: 'user' } ] const newArr = a.reduce((acc, val) => { const findIndex = acc.findIndex(f => f.id === val.id); if (findIndex > -1) { if ((typeof acc[findIndex].role === 'string')) { acc[findIndex].role = [acc[findIndex].role, val.role] } else { acc[findIndex].role.push(val.role) } } else { acc.push(val) } return acc }, []); console.log(newArr)

使用减速器可以非常简单地完成:

 const a = [ { id: 1, name: 'John', role: 'admin' }, { id: 1, name: 'John', role: 'user' }, { id: 2, name: 'Max', role: 'user' } ] const b = a.reduce((acc, el)=>{ const existingEl = acc.find(accEl=>accEl.id === el.id) if(existingEl) existingEl.role.push(el.role) // a very inelegant way of building a shallow copy with // a bit of a data structure change else acc.push({id: el.id, name: el.name, role:[el.role]}) return acc }, []) console.log(b)

您可以遍历输入中的每个项目,将其数据存储在由项目的id属性键入的对象上。 在迭代期间使用Set收集角色可确保最终结果中不存在重复:

 function mergeRoles (users) { const merged = {}; for (const {id, name, role} of users) { (merged[id] ??= {id, name, role: new Set([role])}).role.add(role); } return Object.values(merged).map(user => ({...user, role: [...user.role]})); } const input = [ { id: 1, name: 'John', role: 'admin' }, { id: 1, name: 'John', role: 'user' }, { id: 2, name: 'Max', role: 'user' }, ]; const result = mergeRoles(input); console.log(result);

对于这样的问题,我通常将数组转换为对象字典以合并所有重复项,然后将字典转换回数组:

 const a = [{ id: 1, name: 'John', role: 'admin' }, { id: 1, name: 'John', role: 'user' }, { id: 2, name: 'Max', role: 'user' } ]; // Merge duplicates using object dictionary. let itemsById = {}; for (let item of a) { if (!itemsById[item.id]) { // Id not seen yet. item.role = [item.role]; itemsById[item.id] = item; } else { // Duplicate Id. itemsById[item.id].role.push(item.role); } } // Convert object dictionary back to array. let newArray = []; for (const id in itemsById) { let item = itemsById[id]; if (item.role.length == 1) { item.role = item.role[0]; } newArray.push(item); } console.log(newArray);

暂无
暂无

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

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