如何按其属性值对javascript中的对象数组进行排序?

时间:2019-02-21 12:45:34

标签: javascript arrays json sorting associative-array

具有如下对象数组:

我想按其sortOrder对itemsArray进行排序。

这是项目的sortOrder( itemsArray ['item = a37d2733-f710-4e47-8573-6aab11f6d020']。sortOrder

{
"Items": [
  {
    "sectionId": "section=38",
    "itemsArray": {
      "item=a37d2733-f710-4e47-8573-6aab11f6d020": {
        "itemObject": {
          "description": "Really good food.",
          "price": 1000,
          "name": "Cappuccino",
          "SKey": "item=a37d2733-f710-4e47-8573-6aab11f6d020",
        },
        "sortOrder": 3
      },
      "item=bd844a27-0a00-445d-a40a-785cf16fwine": {
        "itemObject": {
          "description": "Really good wine.",
          "price": 3000,
          "name": "Red Wine",
          "SKey": "item=bd844a27-0a00-445d-a40a-785cf16fwine",
        },
        "sortOrder": 1
      },
      "item=a37d2733-f710-4e47-8573-6aab11f6d016": {
        "itemObject": {
          "description": "Really good drink.",
          "price": 1000,
          "name": "Orange Juice",
          "SKey": "item=a37d2733-f710-4e47-8573-6aab11f6d016",
        },
        "sortOrder": 2
      },
      "item=bd844a27-0a00-445d-a40a-785cf16f4950": {
        "itemObject": {
          "description": "Really good drink.",
          "price": 1000,
          "name": "Coca-Cola - sanjy",
          "SKey": "item=bd844a27-0a00-445d-a40a-785cf16f4950",
        },
        "sortOrder": 0
      }
    },
    "description": "Non-alcoholic beverages.",
    "sectionName": "Beverages",
  }
 ]
}

排序数组后将如下所示:

{
 "Items": [
  {
  "sectionId": "section=38",
  "itemsArray": {
    "item=bd844a27-0a00-445d-a40a-785cf16f4950": {
      "itemObject": {
        "description": "Really good drink.",
        "price": 1000,
        "name": "Coca-Cola - sanjy",
        "SKey": "item=bd844a27-0a00-445d-a40a-785cf16f4950",
      },
      "sortOrder": 0
    },
    "item=bd844a27-0a00-445d-a40a-785cf16fwine": {
      "itemObject": {
        "description": "Really good wine.",
        "price": 3000,
        "name": "Red Wine",
        "SKey": "item=bd844a27-0a00-445d-a40a-785cf16fwine",
      },
      "sortOrder": 1
    },
    "item=a37d2733-f710-4e47-8573-6aab11f6d016": {
      "itemObject": {
        "description": "Really good drink.",
        "price": 1000,
        "name": "Orange Juice",
        "SKey": "item=a37d2733-f710-4e47-8573-6aab11f6d016",
      },
      "sortOrder": 2
    },
    "item=a37d2733-f710-4e47-8573-6aab11f6d020": {
      "itemObject": {
        "description": "Really good food.",
        "price": 1000,
        "name": "Cappuccino",
        "SKey": "item=a37d2733-f710-4e47-8573-6aab11f6d020",
      },
      "sortOrder": 3
    }
  },
  "description": "Non-alcoholic beverages.",
  "sectionName": "Beverages",

}
]
}

我已经在尝试使用 array.sort 方法,但是没有成功。因此,如果有人有解决方案,那就很有帮助。

2 个答案:

答案 0 :(得分:2)

我会考虑将JSON重构为可排序的,例如:

{
  "Items": [
    {
      "sectionId": "section=38",
      "sectionName": "Beverages",
      "description": "Non-alcoholic beverages.",
      "itemsArray": [
        {
          "id": "item=a37d2733-f710-4e47-8573-6aab11f6d020",
          "description": "Really good food.",
          "price": 1000,
          "name": "Cappuccino",
          "sortOrder": 3
        },
        {
          "id": "item=bd844a27-0a00-445d-a40a-785cf16fwine",
          "description": "Really good wine.",
          "price": 3000,
          "name": "Red Wine",
          "sortOrder": 1
        },
        {
          "id": "item=a37d2733-f710-4e47-8573-6aab11f6d016",
          "description": "Really good drink.",
          "price": 1000,
          "name": "Orange Juice",
          "sortOrder": 2
        },
        {
          "id": "item=bd844a27-0a00-445d-a40a-785cf16f4950",
          "description": "Really good drink.",
          "price": 1000,
          "name": "Coca-Cola - sanjy",
          "sortOrder": 0
        }
      ]
    }
  ]
}

可以使用Items[0].itemsArray.sort((a, b) => a.sortOrder - b.sortOrder)进行升序排序。

更改包括将itemsArray设置为数组,并将itemid移到itemsArray的每个项目中自己的属性中。


我不建议按对象排序,因为历史上对对象键的排序顺序没有保证。但是下面的函数可能会按照this Stack Overflow answer的格式对您当前构建的json对象进行排序:

function sortObject(o) {
    var sorted = {},
    key, a = [];

    for (key in o) {
        if (o.hasOwnProperty(key)) {
            a[o[key].sortOrder] = key;
        }
    }

    for (key = 0; key < a.length; key++) {
        sorted[a[key]] = o[a[key]];
    }
    return sorted;
}

json.Items[0].itemsArray = sortObject(json.Items[0].itemsArray);

请注意,这假定您的json对象具有sortOrder的{​​{1}}属性,因为它通过添加到0...n而不是array[sortOrder]来填充数组,然后将该数组迭代为填充结果对象。

答案 1 :(得分:0)

尝试一下,看看并阅读评论

var data = {
"Items": [
  {
    "sectionId": "section=38",
    "itemsArray": {
      "item=a37d2733-f710-4e47-8573-6aab11f6d020": {
        "itemObject": {
          "description": "Really good food.",
          "price": 1000,
          "name": "Cappuccino",

        },
        "sortOrder": 3
      },
      "item=bd844a27-0a00-445d-a40a-785cf16fwine": {
        "itemObject": {
          "description": "Really good wine.",
          "price": 3000,
          "name": "Red Wine",

        },
        "sortOrder": 1
      },
      "item=a37d2733-f710-4e47-8573-6aab11f6d016": {
        "itemObject": {
          "description": "Really good drink.",
          "price": 1000,
          "name": "Orange Juice",
        },
        "sortOrder": 2
      },
      "item=bd844a27-0a00-445d-a40a-785cf16f4950": {
        "itemObject": {
          "description": "Really good drink.",
          "price": 1000,
          "name": "Coca-Cola - sanjy",

        },
        "sortOrder": 0
      }
    },
    "description": "Non-alcoholic beverages.",
    "sectionName": "Beverages",
  }
 ]
}


data.Items.forEach((item)=> {
var array = item.itemsArray;
// sort the array. 
var keys=  Object.keys(array).sort((a, b)=> {
    return array[a].sortOrder -  array[b].sortOrder
  });
  
  // and now create the new itemsArray
  var _itemsArray= {};
  keys.forEach((key)=> _itemsArray[key] =array[key])
  // last assign the itemsArray
  item.itemsArray = _itemsArray;
});

console.log(data.Items)