寻找一种在javascript中对对象数组进行排序的方法

Looking for a way to sort an array of objects in javascript

本文关键字:数组 对象 排序 方法 一种 javascript 寻找      更新时间:2023-09-26

所以我有一个看起来像这样的JSON数组:

[
  {
    row: [
      {
        boat: {
          description: 'Books',
          version: '2',
          id: 6
        },
        airplanes: [
          {
            airplane: [
              {
                description: 'DVD',
                version: 2,
                uid: 69,
                wid: 65,
                id: 84
              }
            ],
            trains: {
              train: [
                {
                  description: 'Pictures',
                  version: 2,
                  id: 149
                }
              ],
              actions: [
                {
                  description: 'This is a really long sentence.',
                  version: 2,
                  tid: 69.01,
                  id: 452
                },
                {
                  description: 'article 2',
                  version: 2,
                  tid: 69.02,
                  id: 453
                },
                {
                  description: 'developer 1',
                  version: 2,
                  tid: 69.03,
                  id: 454
                }
              ]
            }
          },
          {
            airplane: [
              {
                description: 'Games',
                version: 2,
                uid: 65,
                wid: 61,
                id: 80
              }
            ],
            trains: {
              train: [
                {
                  description: 'another great descriptions.',
                  version: 2,
                  id: 145
                }
              ],
              actions: [
                {
                  description: 'being indecisive is good.',
                  version: 2,
                  tid: 65.01,
                  id: 442
                },
                {
                  description: 'couches are comfortable',
                  version: 2,
                  tid: 65.02,
                  id: 443
                }
              ]
            }
          }
        ]
      }
    ]
  }
]

我正在尝试按升序按"wid"对上述输出进行排序,但仍然能够保留整体顺序。例如,在上面,数组元素 0 中的 wid 是 65,数组的元素 1 中的 wid 值是 61。因此,应交换元素 1 和元素 0。有没有内置的javascript方法来像这样对json进行排序?

我将有一个比提供的示例大得多的 json 数组输出。

Underscore 和 LoDash 都有一个排序方法,可以完成你正在寻找的事情。在这个例子中,我假设你显示的数据结构存储在一个名为data的变量中。

 _.each(data, function(obj) {
     _.each(obj.row, function(row) {
        // note the reassignment, _.sortBy does not sort in-place
        row.airplanes = _.sortBy(row.airplanes, function(airplane) {
           return airplane.wid; // This will sort ascending. 
                                // To sort descending, simply multiply by -1
        });
     });
 });

那么这是在做什么呢?它获取根数据结构中的每个数组元素并循环访问它(这是第一个_.each)。然后,在每个对象中,它循环访问每个row元素,并按每个元素中包含的wid元素对数组进行排序row.airplanes

希望这对您有所帮助。顺便说一句,您发布的数据是严格无效的 JSON。每个键都应该用双引号引起来,即"row",而不仅仅是row,单引号对于分隔字符串无效,即"DVD"而不是'DVD'。此外,boat版本是字符串,而其他版本标识符是整数,最好尝试将版本标识符保留为整数。

我建议使用优秀的jq命令行JSON处理器。非常快,因为它是用便携式C编写的。 易于理解的文档。

cat <file.json> | jq . -S