2 回答

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超4個(gè)贊
使用object.Values和reduce來(lái)刪除重復(fù)項(xiàng)并獲取version
每個(gè)的最大值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貢獻(xiàn)1775條經(jīng)驗(yàn) 獲得超8個(gè)贊
您可以通過(guò)使用來(lái)實(shí)現(xiàn)結(jié)果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)
添加回答
舉報(bào)