【发布时间】:2018-04-19 12:04:20
【问题描述】:
无法理解如何对这种嵌套数组中包含数组的数据进行排序...
var data = {
d1: [{
nest1: {a:5, b:2},
nest2: {a:3, b:1},
}],
d3: [{
nest1: {a:7, b:9},
nest2: {a:1, b:22},
}],
d2: [{
nest1: {a:1, b:9},
nest2: {a:11, b:22},
}],
}
所以基本上,想按 a、b、key 排序。
所以首先,我已经迭代了所有对象以获得一个数组(能够按键排序)
现在数据看起来像(只是示例)
data = [{
key: "d1",
obj: [{
nest1: {a:1, b:2},
nest2: {a:3, b:12},
}]}],
[{
key: "d2",
obj: [{
nest1: {a:11, b:12},
nest2: {a:13, b:22},
}]}]
所以现在我可以轻松地按键排序了 -
_.orderBy(vm.summaryData, 'key')
但还是一头雾水,怎么按data.obj.nest1.a排序??
期望:按key本身排序:
data = {
d1: [{
nest1: {a:5, b:2},
nest2: {a:3, b:1},
}],
d2: [{
nest1: {a:1, b:9},
nest2: {a:11, b:22},
}],
d3: [{
nest1: {a:7, b:9},
nest2: {a:1, b:22},
}],
}
期望:按nest1.a排序:
data = {
d2: [{
nest1: {a:1, b:9},
nest2: {a:11, b:22},
}],
d1: [{
nest1: {a:5, b:2},
nest2: {a:3, b:1},
}],
d3: [{
nest1: {a:7, b:9},
nest2: {a:1, b:22},
}],
}
期望:按nest2.a排序:
data = {
d3: [{
nest1: {a:7, b:9},
nest2: {a:1, b:22},
}],
d1: [{
nest1: {a:5, b:2},
nest2: {a:3, b:1},
}],
d2: [{
nest1: {a:1, b:9},
nest2: {a:11, b:22},
}],
}
等等...nest1.b、nest2.b.....
【问题讨论】:
-
这种情况下的排序是什么意思?请添加更多示例或更多示例。
-
好的,但是已经给出了例子/案例。
-
数据不是有效的 javascript 对象
-
抱歉错字,已更正。
-
感谢 Nina,也添加了预期结果。
标签: javascript arrays sorting underscore.js lodash