Javascript 如何按嵌套对象值对具有唯一键的对象数组进行排序
真的很难找到解决办法。我有以下数据:Javascript 如何按嵌套对象值对具有唯一键的对象数组进行排序,javascript,sorting,Javascript,Sorting,真的很难找到解决办法。我有以下数据: var arr = [ { Main: {type: "object", ...} } { Poller: {type: "object", ...} } { connection: {type: "array", ...} } { Profile1: {type: "array", ...} } { Sender: {type: "obj
var arr = [
{ Main: {type: "object", ...} }
{ Poller: {type: "object", ...} }
{ connection: {type: "array", ...} }
{ Profile1: {type: "array", ...} }
{ Sender: {type: "object", ...} }
{ Profile2: {type: "array", ...} }
];
我的目标是生成一个数组,首先列出type:array
的对象。我尝试了sort()方法的各种实现,但不知道如何说明每个嵌套对象都有自己的键。任何这样的帮助都是非常感谢的
var-arr=[
{Main:{type:“object”,}},
{Poller:{type:“object”,}},
{连接:{type:“array”,}},
{Profile1:{type:“array”,}},
{Func1:{type:“function”,}},
{func2:{type:“function”,}},
{发送方:{type:“object”,}},
{Profile2:{type:“array”,}}
];
arr.sort((a,b)=>{
if(Object.values(a)[0].typeObject.values(b)[0].type)返回1
})
console.log(arr)
请添加您的尝试。?arr.sort((a,b)=>(Object.values(b)[0]。type==“array”)-(Object.values(a)[0]。type==“array”)
@gorak我不知道Object.values,这很完美,谢谢。IDK如何将你的答案标记为correct@AlecSelinger我在Gorak评论的时候给你写了一个答案。。。