获取JavaScript数组中重复项的数组

huangapple go评论64阅读模式
英文:

get array of duplicate item in list JavaScript array

问题

这是您提供的代码,按您要求进行翻译:

我有一个对象列表想要根据一个字段年龄将它们分开并按从小到大的顺序分类

var data = [
  {
    name: "Mike",
    age: 10
  },
  {
    name: "Matt",
    age: 10
  },
  {
    name: "Nancy",
    age: 15
  },
  {
    name: "Adam",
    age: 22
  },
  {
    name: "Jenny",
    age: 22
  }
];

结果应该类似于这样:

[
[{name: "Mike", age: 10}, {name: "Matt", age: 10}],
[{name: "Nancy", age: 15}],
[{name: "Adam", age: 22}, {name: "Jenny", age: 22}]
]
英文:

I have a list of objects and I want to separate them based on a field(age) and categorize them from small to large.

var data = [
  {
    name: "Mike",
    age: 10
  },
  {
    name: "Matt"
    age: 10
  },
  {
    name: "Nancy",
    age: 15
  },
  {
    name: "Adam",
    age: 22
  },
  {
    name: "Jenny",
    age: 22
  },
 
];

The result should be something like this

[
[{name:"Mike",age: 10},{name: "Matt"age: 10}],
[{name: "Nancy",age: 15}],
[{name: "Adam", age: 22},{name: "Jenny",age: 22}]
 ]

答案1

得分: 2

有多种方法可以处理这样的问题。其中一种方法是使用 .reduce() 方法按年龄分组数据。

var groupedData = data.reduce(function (result, currentValue) {
  if (!result[currentValue.age]) {
    result[currentValue.age] = [];
  }
  result[currentValue.age].push(currentValue);
  return result;
}, {});

该函数对数组中的每个元素进行调用,将结果累积到 result 变量中。如果年龄是之前没有遇到过的,我们会创建一个空数组并将值添加到其中。

groupedData 对象的初始值是 {}。如果你使用 console.log 打印它,你会看到:

{10: Array(2), 15: Array(1), 22: Array(2)}

如果你想让 groupedData 变成一个数组,你可以使用 Object.values()。它会将对象中的数组转换为一个数组。

groupedData = Object.values(groupedData);
英文:

There are multiple ways to approach a problem like this. One way is to use the .reduce() method to group the data by age.

var groupedData = data.reduce(function (result, currentValue) {
  if (!result[currentValue.age]) {
    result[currentValue.age] = [];
  }
  result[currentValue.age].push(currentValue);
  return result;
}, {});

The function is called for each element in the array, accumulating the results in the result variable. If the age is one we haven't encountered before, we create an empty array and add the value to it.

The initial value of the object groupedData is {}. If you console.log it, you will see:

{10: Array(2), 15: Array(1), 22: Array(2)}.

if you want groupedData to be an array you can use Object.values(). It will transform the object of arrays into an array of arrays.

groupedData = Object.values(groupedData);

答案2

得分: 1

函数groupByAge将完全满足您的需求。

var data = [
  {
    name: "Mike",
    age: 10
  },
  {
    name: "Matt",
    age: 10
  },
  {
    name: "Adam",
    age: 22
  },
  {
    name: "Nancy",
    age: 15
  },
  {
    name: "Jenny",
    age: 22
  },
];

function groupByAge(data) {
	
  const sortedData = data.sort((a, b) => {
  	if (a.age > b.age) return 1;
    if (a.age < b.age) return -1;
    return 0;
  });
  
  const returnData = [];
  
  sortedData.forEach((item) => {
  	const foundedArray = returnData.find((elem) => elem.find((elemB) => elemB.age === item.age));
    if (foundedArray) {
    	foundedArray.push(item);
    } else {
    	returnData.push([item]);
    }
  });
  
  return returnData;
}

console.log(groupByAge(data));

英文:

The function groupByAge is gonna do exactly what you need.

var data = [
  {
    name: &quot;Mike&quot;,
    age: 10
  },
  {
    name: &quot;Matt&quot;,
    age: 10
  },
  {
    name: &quot;Adam&quot;,
    age: 22
  },
  {
    name: &quot;Nancy&quot;,
    age: 15
  },
  {
    name: &quot;Jenny&quot;,
    age: 22
  },
];

function groupByAge(data) {
	
  const sortedData = data.sort((a, b) =&gt; {
  	if (a.age &gt; b.age) return 1;
    if (a.age &lt; b.age) return -1;
    return 0;
  });
  
  const returnData = [];
  
  sortedData.forEach((item) =&gt; {
  	const foundedArray = returnData.find((elem) =&gt; elem.find((elemB) =&gt; elemB.age === item.age));
    if (foundedArray) {
    	foundedArray.push(item);
    } else {
    	returnData.push([item]);
    }
  });
  
  return returnData;
}

console.log(groupByAge(data));

huangapple
  • 本文由 发表于 2023年3月3日 21:14:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75627552.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定