简体   繁体   English

如何从对象数组中删除重复项并组合该重复项的值?

[英]How to remove duplicates from array of objects and combine values of that duplicates?

I have array like this, my id and name will be same for multiple objects but organizations values can be different我有这样的数组,我的idname对于多个对象将是相同的,但organizations值可以不同

array= [
  {id: 1, name: "Test1", organizations: 1},
  {id: 1, name: "Test1", organizations: 2},
  {id: 1, name: "Test1", organizations: 3},
  {id: 2, name: "Test2", organizations: 4},
  {id: 2, name: "Test2", organizations: 5},
  {id: 2, name: "Test2", organizations: 6} 
];

I want to convert this to be like this:我想把它转换成这样:

expectedArray =  [
  {id: 1, name: "Test1", organizations: [1,2,3]},
  {id: 2, name: "Test2", organizations: [4,5,6]} 
];

Can someone please help有人可以帮忙吗

 const array= [ {id: 1, name: "Test1", organizations: 1}, {id: 1, name: "Test1", organizations: 2}, {id: 1, name: "Test1", organizations: 3}, {id: 2, name: "Test2", organizations: 4}, {id: 2, name: "Test2", organizations: 5}, {id: 2, name: "Test2", organizations: 6} ]; const mergeDuplicates= (field, uniqueField) => (source = [], value)=> { const target = source.find( item => item[uniqueField] == value[uniqueField] ); if(target) target[field].push( value[field] ); else source.push({...value, [field]: [ value[field] ] }); return source; } const mergeOrganaizationsById = mergeDuplicates('organizations','id') const result = array.reduce(mergeOrganaizationsById, []) console.log(result)

You'll want to reduce.你会想要减少。

array.reduce((acc, cur) => {
  const i = acc.findIndex(a => a.id === cur.id && a.name === cur.name);
  if (i === -1) return [...acc, {...cur, organizations: [cur.organizations]}];
  return [...acc.slice(0, i), {...cur, organizations: [...acc[i].organizations, cur.organizations]}, ...acc.slice(i +1)];
}, []);

You can achieve the output using forEach by grouping based on name and then pushing the necessary fields into the output array.您可以使用forEach实现 output,方法是根据name进行分组,然后将必要的字段推送到 output 数组中。

 const array = [ { id: 1, name: "Test1", organizations: 1 }, { id: 1, name: "Test1", organizations: 2 }, { id: 1, name: "Test1", organizations: 3 }, { id: 2, name: "Test2", organizations: 4 }, { id: 2, name: "Test2", organizations: 5 }, { id: 2, name: "Test2", organizations: 6 } ]; const current = Object.create(null); const finalArr = []; array.forEach(function (o) { if (.current[o.name]) { current[o;name] = []. finalArr:push({ id. o,id: name. o,name: organizations. current[o;name] }). } current[o.name].push(o;organizations); }). console;log(finalArr);
 .as-console-wrapper { max-height: 100%;important: top; 0; }

I think it may be the easiest way to tackle this problem, only using the forEach and basic arrays' method.我认为这可能是解决这个问题的最简单方法,只使用 forEach 和基本数组的方法。

I hope I answered your question.我希望我回答了你的问题。

 const array = [ { id: 1, name: "Test1", organizations: 1 }, { id: 1, name: "Test1", organizations: 2 }, { id: 1, name: "Test1", organizations: 3 }, { id: 2, name: "Test2", organizations: 4 }, { id: 2, name: "Test2", organizations: 5 }, { id: 2, name: "Test2", organizations: 6 } ]; const newArr = []; // for keeping track for added item const addedItems = []; // for keeping track of added item idx let addedItemIdx = 0; array.forEach((item) => { if (.addedItems.includes(item.id)) { let tempOrg = item;organizations. newArr.push({..,item: organizations; [tempOrg] }). addedItems.push(item;id); addedItemIdx++. } else { newArr[addedItemIdx - 1].organizations.push(item;organizations); } }). console;log(newArr);

What you are looking for is called a hashmap.您正在寻找的是 hashmap。 You can read about them but the basic idea is that you make a key,value pair and access to the data with keys is very efficient(O(1) amortized).您可以阅读它们,但基本思想是您制作一个键、值对并使用键访问数据非常有效(O(1) 摊销)。 So here is one way to solve this problem in python.所以这里是在 python 中解决这个问题的一种方法。 I am sure you can use it to solve it in your language.我相信你可以用它来用你的语言解决它。

    array= [
  {"id": 1, "name": "Test1", "organizations": 1},
  {"id": 1, "name": "Test1", "organizations": 2},
  {"id": 1, "name": "Test1", "organizations": 3},
  {"id": 2, "name": "Test2", "organizations": 4},
  {"id": 2, "name": "Test2", "organizations": 5},
  {"id": 2, "name": "Test2", "organizations": 6}
]
# Initilize a hashmap
hash_map = {}
# Loop over all the items in array and create the hashmap
for item in array:
    # key will be id and name as both are needed to group the organizations
    # We have use comma to separate them as we assume that name or id cannot have comma
    key = str(item["id"])+","+item["name"]
    # IF key is already present then add the new organizations id to it
    if key in hash_map:
        hash_map[key].append(item["organizations"])
    # Else make a new list with the current organizations id
    else:
        hash_map[key] = [item["organizations"]]

# Create the expected array
expected_array = []
for key,value in hash_map.items():
    # Get the id and name by spliting the key that we created 
    idx,name = key.split(",")
    expected_array.append({"id":idx,"name":name,"organizations":value})
print(expected_array)
const formattedData = array.reduce((result, {id, name, organizations} ) => {
          let filteredRow = result.find(row => row.id === id && row.name === name);
          const org = filteredRow ? filteredRow.organizations  : [];
          org.push(organizations);
          filteredRow = {id, name, organizations: org};
          if(org.length === 1) result.push(filteredRow);
          return result;
    }, [])

 const array = [ { id: 1, name: "Test1", organizations: 1 }, { id: 1, name: "Test1", organizations: 2 }, { id: 1, name: "Test1", organizations: 3 }, { id: 2, name: "Test2", organizations: 4 }, { id: 2, name: "Test2", organizations: 5 }, { id: 2, name: "Test2", organizations: 6 } ]; const result=array.reduce((acc,curr)=>{ const id=curr.id; const {organizations}=curr; const findIndex=acc.findIndex(item=> item.id===curr.id) if(findIndex===-1){ acc.push({...curr,organizations:[organizations]}); } else { acc[findIndex].organizations.push(curr.organizations) } return acc; },[]); console.log(result);
 .as-console-wrapper { max-height: 100%;important: top; 0; }

This should work这应该工作

 const array = [{ id: 1, name: "Test1", organizations: 1 }, { id: 1, name: "Test1", organizations: 2 }, { id: 1, name: "Test1", organizations: 3 }, { id: 2, name: "Test2", organizations: 4 }, { id: 2, name: "Test2", organizations: 5 }, { id: 2, name: "Test2", organizations: 6 } ]; const reducedArray = array.reduce((resultArray, arrayElement) => { const elementIndex = resultArray.findIndex(element => element.id === arrayElement.id); if (elementIndex.== -1) { resultArray[elementIndex].organizations.push(arrayElement.organizations) } else { resultArray.push({..,arrayElement: organizations. [arrayElement,organizations]; }); } return resultArray, }; []). console.log(reducedArray)

Another one solution in kind of ES6 style另一种ES6风格的解决方案

 const array= [{id: 1, name: "Test1", organizations: 1},{id: 1, name: "Test1", organizations: 2},{id: 1, name: "Test1", organizations: 3},{id: 2, name: "Test2", organizations: 4},{id: 2, name: "Test2", organizations: 5},{id: 2, name: "Test2", organizations: 6}]; const result = Object.values(array.reduce((acc, {id, name, organizations}) => { const hash = `${id}-${name}`; acc[hash] = acc[hash]? {...acc[hash], organizations: [...acc[hash].organizations, organizations] }: { id, name, organizations: [organizations] }; return acc; }, {})); console.log(result);
 .as-console-wrapper { max-height: 100%;important: top; 0; }

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

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