简体   繁体   English

按属性数组对对象数组进行排序

[英]Sorting an array of objects by property array

I got an array of objects like this one:我得到了一组像这样的对象:

var SkillBuddys = [
    {
        Property1: "1",
        Property2: "Test",
        Property3: "Data",
        Property4: [{},{},{}],
    }, {
        Property1: "2",
        Property2: "Test2",
        Property3: "Data2"
    }, {
        Property1: "3",
        Property2: "Test3",
        Property3: "Data3",
        Property4: [{},{}],
    }, {
        Property1: "4",
        Property2: "Test4",
        Property3: "Data4"
    }, {
        Property1: "5",
        Property2: "Test5",
        Property3: "Data5",
        Property4: [{}],
    }
];

I want to sort it with javaScript on Property 4, so if the object has the property 4 and contains an array.我想在属性 4 上使用 javaScript 对其进行排序,因此如果对象具有属性 4 并包含一个数组。 Like this:像这样:

var SkillBuddys = [
    {
        Property1: "1",
        Property2: "Test",
        Property3: "Data",
        Property4: [{},{},{}],
    }, {
        Property1: "3",
        Property2: "Test3",
        Property3: "Data3",
        Property4: [{},{}],
    }, {
        Property1: "5",
        Property2: "Test5",
        Property3: "Data5",
        Property4: [{}],
    }, {
        Property1: "2",
        Property2: "Test2",
        Property3: "Data2"
    }, {
        Property1: "4",
        Property2: "Test4",
        Property3: "Data4"
    }
];

If Property 4 does not exists it returns "Undefined".如果属性 4 不存在,则返回“未定义”。 How can i sort this with Array.Sort?如何使用 Array.Sort 对此进行排序?

You could sort array by using length property and ||您可以使用length属性和|| sort数组进行sort operator.操作员。

 var array = [ { Property1: "1", Property2: "Test", Property3: "Data", Property4: [{},{},{}], }, { Property1: "2", Property2: "Test2", Property3: "Data2" }, { Property1: "3", Property2: "Test3", Property3: "Data3", Property4: [{},{}], }, { Property1: "4", Property2: "Test4", Property3: "Data4" }, { Property1: "5", Property2: "Test5", Property3: "Data5", Property4: [{}], } ]; array.sort((a, b) => (b['Property4'] || []).length - (a['Property4'] || []).length); console.log(array);

One approach could be to destructure and then use a default array if the property doesn't exist.一种方法可能是解构,然后在属性不存在时使用默认数组。 You can then .sort() by the difference in the array lengths:然后您可以通过数组长度的差异.sort()

 const skillBuddys = [ { Property1: "1", Property2: "Test", Property3: "Data", Property4: [{},{},{}], }, { Property1: "2", Property2: "Test2", Property3: "Data2" }, { Property1: "3", Property2: "Test3", Property3: "Data3", Property4: [{},{}], }, { Property1: "4", Property2: "Test4", Property3: "Data4" }, { Property1: "5", Property2: "Test5", Property3: "Data5", Property4: [{}], } ]; const res = skillBuddys.sort( ({Property4: a = []}, {Property4: b = []}) => b.length - a.length ); console.log(res);

SkillBuddys
.filter((x) => Array.isArray(x.Property4)  )
.sort( (a,b) => b.Property4.length - a.Property4.length)
.concat( SkillBuddys.filter((x) => !Array.isArray(x.Property4))

This will give you an array with the elements with Property4 sorted by lenght of the inner array, and with the rest of the elements afterwards in no specified ordering (you could pipe a sort() with an ordering of your choice)这将为您提供一个数组,其中包含 Property4 的元素按内部数组的长度排序,然后其余元素没有指定的顺序(您可以使用您选择的顺序对 sort() 进行管道传输)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM