简体   繁体   English

如何将一组对象合并到一个数组中,然后过滤掉重复项

[英]How to merge an array of objects into one array and then filter out the duplicates

Firstly, I am trying to merge an array of many objects into a single array with every key in each object.首先,我试图将许多对象的数组合并到一个数组中,每个数组中的每个键都在 object 中。

Lastly, any duplicate items in the array should be removed as well as any elements named "name".最后,应删除数组中的任何重复项以及任何名为“name”的元素。

Input:输入:

const data = [
  {
    name: '10/20',
    Tyler: 1,
    Sonia: 0,
    Pedro: 0,
  },
  {
    name: '10/23',
    Tyler: 0.5,
    Sonia: 0.25,
    Pedro: 0.75,
    George: 0.5,
  },
];

Output: Output:

["Tyler", "Sonia", "Pedro", "George"]

This is what I've tried so far:到目前为止,这是我尝试过的:

const mergedData = data.reduce((prev, cur) => {
  const obj = cur[0];
  const keys = Object.keys(obj);
  const names = keys.splice(1);
  return { names };
}, []);

I am trying to capture any key name other than "name" and add it to the final array.我正在尝试捕获除“name”之外的任何键名并将其添加到最终数组。 However, this is where I get stuck because I get this error, TypeError: Cannot convert undefined or null to object但是,这是我卡住的地方,因为我收到此错误, TypeError: Cannot convert undefined or null to object

Note: Objects may be different lengths, contain a mix of names, but never any duplicates.注意:对象可能有不同的长度,包含混合的名称,但绝不会重复。

An option is to find all keys put in a set and remove the name key一个选项是找到放在一个集合中的所有键并删除name

 const data = [ { name: '10/20', Tyler: 1, Sonia: 0, Pedro: 0, }, { name: '10/23', Tyler: 0.5, Sonia: 0.25, Pedro: 0.75, George: 0.5, }, ]; const set = new Set(data.reduce((acc, i) => [...acc, ...Object.keys(i)], [])); set.delete('name'); const result = [...set]; console.log(result);

 const data = [ { name: '10/20', Tyler: 1, Sonia: 0, Pedro: 0, }, { name: '10/23', Tyler: 0.5, Sonia: 0.25, Pedro: 0.75, George: 0.5, }, ]; const result =Array.from(new Set(data.reduce((acc, i) => [...acc, ...Object.keys(i)], []))); console.log(result)

If you have access to ES6 methods, you can do this using a Set (unique values are ensured at creation) and converting it back into an array if you want through Destructuring .如果您可以访问 ES6 方法,则可以使用Set (在创建时确保唯一值)来执行此操作,如果需要,可以通过Destructuring将其转换回数组。

 data = [{name: '0', Tyler: '1', Dan: '2', Carl: '3'}, {name: '0', Tyler: '1', Dan: '2', Eric: '3', Danny: '4'}]; const output = (data) => { let output = []; // This makes sure you get each array and then strips just the keys as desired data.forEach(item => { output = output. concat(Object.keys(item)) }); // This creates the set, strips our dups, and then spreads itself into an array return [...new Set(output)] // Strip out the 'name' key as needed // NOTE: This should be a param instead of hard-coded, but this is easier to show .filter(res => res != 'name'); } console.log(output(data));

This should be fairly performant considering it only navigates the full array one time and each object itself shouldn't have millions of properties to cause .keys() any issues.考虑到它只导航整个数组一次,并且每个对象本身不应该有数百万个属性来导致.keys()任何问题,这应该是相当.keys()

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

相关问题 如何过滤出对象数组的重复项 - How to filter out Duplicates of an array of objects Javascript合并对象数组,没有重复 - Javascript merge array of objects, no duplicates 如果一个值相同,则过滤掉数组中的对象 - Filter out objects in array if one value is the same 如何合并对象数组中的重复项并对 MongoDB 中的特定属性求和? - How to merge duplicates in an array of objects and sum a specific property in MongoDB? 我如何将这个重复的对象合并到一个数组 JS 中 - How can i merge this duplicates objects in an array JS 如何将带有嵌套对象的 2 个 Arrays 合并到 1 个没有重复的数组中 - How to merge 2 Arrays with nested Objects into 1 array without duplicates 从 JavaScript 中的一组对象中删除重复项但在删除重复项之前合并一个字段 - Remove duplicates from an array of objects in JavaScript But merge one field before removing the duplicates 如何在 javascript 中的一个数组中合并另一个数组对象中的数组 - how to merge array inside another array objects in one array in javascript 如何通过比较其中的一个 object 来合并对象数组 - how to merge an array of objects by comparing one object in it 如何过滤掉 JSON 数组中的嵌套对象? - How to filter out nested objects in JSON array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM