假设我有这个 javascript 对象数组,
[{a:'a', b:2, c:true}, {a:'b', b:3, c:true}, {a:'a1', b:3, false}]
假设我需要将索引 0 处的对象移动到 2。我尝试过这个函数,但没有成功。
Array.prototype.move = function (old_index, new_index) {
if (new_index >= this.length) {
var k = new_index - this.length;
while ((k--) + 1) {
this.push(undefined);
}
}
this.splice(new_index, 0, this.splice(old_index, 1)[0]);
};
最佳答案
对象数组不正确,它需要最后一项中的“c”变量:
[{a:'a', b:2, c:true}, {a:'b', b:3, c:true}, {a:'a1', b:3, c:false}];
关于javascript - 如何移动/更改 JavaScript 对象数组(JSON)的位置/顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14377867/