【发布时间】:2020-03-04 09:15:01
【问题描述】:
所以我有一个对象数组,例如:
let arrayOfObjects = [
{
Name: "Apple",
Type: "Fruit"
},
{
Name: "Carrot",
Type: "Vegetable"
},
{
Name: "Carrot",
Type: "Vegetable"
},
{
Name: "Carrot",
Type: "Vegetable"
},
{
Name: "Apple",
Type: "Fruit"
},
{
Name: "Apple",
Type: "Fruit"
},
{
Name: "Carrot",
Type: "Vegetable"
}
];
我需要仔细检查它,找到重复项并为每个重复项仅返回一个对象,但将重复项的数量作为新参数。
像这样:
let newArrayOfObjects = [
{
Name: "Apple",
Type: "Fruit",
times: 3,
},
{
Name: "Carrot",
Type: "Vegetable",
times: 4,
}
];
我可以根据一个参数计算重复对象,但我不知道如何根据整个对象来做。解决这个问题的最佳方法是什么?
【问题讨论】:
标签: javascript arrays object filter