寻找一种对 javascript 中的对象数组进行排序的方法
Looking for a way to sort an array of objects in javascript
所以我有一个 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 数组输出比提供的示例大很多。
我推荐使用优秀的jq commandline JSON processor. Extremely fast since it was written in portable C. Easy to understand documentation。
cat <file.json> | jq . -S
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
版本是一个字符串,而您的其他版本标识符是整数,最好尝试将您的版本标识符保持为整数。
所以我有一个 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 数组输出比提供的示例大很多。
我推荐使用优秀的jq commandline JSON processor. Extremely fast since it was written in portable C. Easy to understand documentation。
cat <file.json> | jq . -S
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
版本是一个字符串,而您的其他版本标识符是整数,最好尝试将您的版本标识符保持为整数。