2 回答
TA贡献1836条经验 获得超4个赞
使用object.Values和reduce来删除重复项并获取version
每个的最大值name
。
演示
let my_array = [
{ name: "foo", version: "10" },
{ name: "foo", version: "60" },
{ name: "foo", version: "20" },
{ name: "bar", version: "400" },
];
let unique = Object.values(my_array.reduce((r, x) => {
r[x.name] = r[x.name] && r[x.name].version > x.version ? r[x.name] : x
return r
}, {}))
console.log(unique )
TA贡献1775条经验 获得超8个赞
您可以通过使用来实现结果reduce
let my_array = [{
name: "foo",
version: "10"
},
{
name: "foo",
version: "60"
},
{
name: "foo",
version: "20"
},
{
name: "bar",
version: "400"
},
];
var new_array = my_array.reduce((max, b) => Math.max(max, b.version), my_array[0].version);
console.log(new_array)
添加回答
举报