1.一个JSON数据按某个字段进行排序

例如JSON数据:javascript

let json = [
        {
          index: 2,

          name: "2"
        },
        {
          index: 2,

          name: "2"
        },
        {
          index: 1,

          name: "1"
        },
        {
          index: 3,

          name: "3"
        }
      ];

      json = json.sort((a, b) => {
        return a.index - b.index;
      });

      console.log(json);

解释:按key为index的从小到大排序。java

输出:json

 

相关文章
相关标签/搜索