gpt4 book ai didi

javascript - 组合不同的js对象并按键对它们进行排序

转载 作者:太空宇宙 更新时间:2023-11-04 15:29:54 26 4
gpt4 key购买 nike

如何组合以下三个或更多对象并按“名称”键按字母顺序对它们进行排序?

var obj1 = {name: "BBBB", number: 2, task: "testing"};
var obj2 = {name: "AAAA"};
var obj3 = {name: "CCCC", number: 1};

最佳答案

var obj1 = {
name: "BBBB",
number: 2,
task: "testing"
};
var obj2 = {
name: "AAAA"
};
var obj3 = {
name: "CCCC",
number: 1
};

var obj4 = [obj1, obj2, obj3];
obj4.sort((a, b) => a.name.localeCompare(b.name));
console.log(obj4);

关于javascript - 组合不同的js对象并按键对它们进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44883412/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com