我有以下 json abjects,我喜欢将它们合并到一个对象中,并根据“分钟”值缩短子对象。这对 JavaScript 可行吗?
[Object, Object, Object]
0: Object
Live: "188585"
Minute: "17"
Player: "Player A"
Team: "188564"
__proto__: Object
1: Object
Live: "188585"
Minute: "26"
Player: "Player B"
Team: "188564"
__proto__: Object
2: Object
Live: "188585"
Minute: "77"
Player: "Player A"
Team: "188564"
__proto__: Object
[Object, Object]
0: Object
Live: "188585"
Minute: "31"
Player: "Player C"
Team: "188558"
__proto__: Object
1: Object
Live: "188585"
Minute: "41"
Player: "Player D"
Team: "188558"
__proto__: Object
我喜欢这样的结果:
[Object, Object, Object, Object, Object]
0: Object
Live: "188585"
Minute: "17"
Player: "Player A"
Team: "188564"
__proto__: Object
1: Object
Live: "188585"
Minute: "26"
Player: "Player B"
Team: "188564"
__proto__: Object
2: Object
Live: "188585"
Minute: "31"
Player: "Player C"
Team: "188558"
__proto__: Object
3: Object
Live: "188585"
Minute: "41"
Player: "Player D"
Team: "188558"
__proto__: Object
4: Object
Live: "188585"
Minute: "77"
Player: "Player A"
Team: "188564"
__proto__: Object
最佳答案
更多的是对象,它们看起来像是对象数组。如果是这种情况,您可以使用 Array.concat连接,和Array.sort排序。
类似于:
// assuming `aba` is the first array, `cd` the second
var result = aba.concat(cd);
result.sort(function(a, b) {
return a.Minute - b.Minute // implicit conversion in number
});
console.log(result);
关于JavaScript | JSON 对象按值合并和排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14747795/