JSON数组索引顺序

JSON Array Index Order

本文关键字:顺序 索引 数组 JSON      更新时间:2023-09-26

有一个系统将文本行输入为数组,例如array[123556,"test",0,0]。当使用val().split('''n')时,我可以将每条新行添加到新数组中,这样每条行的索引都会增加1,例如

array[123,556,"test",0,1] = "line 1"
array[123,556,"test",0,2] = "line 2"
array[123,556,"test",0,3] = "line 3"
array[123,556,"test",0,4] = "line 4"

但我需要最后两个索引反向显示。阵列需要看起来像这样:

array[123,556,"test",1,0] = "line 1"
array[123,556,"test",2,0] = "line 2"
array[123,556,"test",3,0] = "line 3"
array[123,556,"test",4,0] = "line 4"

不知怎的,他们能够让数组索引在第4个索引中递增。我只能让它在第五个索引中递增。。我尝试过.push(0)在末尾添加一个0,但出现了错误。

有什么想法吗?

感谢

一种可能性是从数组中拼接(移除)索引,并将其推回到数组的末尾。

var arr = [123,556,"test",0,1];
console.log(arr);  // [123, 556, "test", 0, 1] 
arr.push( arr.splice(3,1)[0] );
console.log(arr); // [123, 556, "test", 1, 0]

http://jsfiddle.net/5wxcd3ux/