【问题标题】:Sorting an array of objects by property array按属性数组对对象数组进行排序
【发布时间】:2019-12-17 13:27:01
【问题描述】:

我得到了一组像这样的对象:

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: [{}],
    }
];

我想在属性 4 上使用 javaScript 对其进行排序,所以如果对象具有属性 4 并包含一个数组。像这样:

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"
    }
];

如果属性 4 不存在,则返回“未定义”。如何使用 Array.Sort 对其进行排序?

【问题讨论】:

    标签: javascript arrays sorting


    【解决方案1】:

    您可以使用length 属性和|| 运算符来sort 数组。

    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);

    【讨论】:

      【解决方案2】:

      如果属性不存在,一种方法可能是解构然后使用默认数组。然后您可以通过数组长度的差异.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);

      【讨论】:

        【解决方案3】:
        SkillBuddys
        .filter((x) => Array.isArray(x.Property4)  )
        .sort( (a,b) => b.Property4.length - a.Property4.length)
        .concat( SkillBuddys.filter((x) => !Array.isArray(x.Property4))
        

        这将为您提供一个数组,其中包含 Property4 的元素按内部数组的长度排序,然后其余元素没有指定的顺序(您可以使用您选择的顺序来管道排序())

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-02-20
          • 2010-11-02
          • 1970-01-01
          • 2011-09-06
          相关资源
          最近更新 更多