英文:
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: "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));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论