如何拆分javascript数组?

时间:2016-07-01 17:09:16

标签: javascript arrays

在python中,我可以像这样删除数组xs中的第一项:

xs = xs[1:]

我怎么能用以下的javascript数组呢?

var data = {{data}}

2 个答案:

答案 0 :(得分:2)

JavaScript splice函数,不是jQuery的特定。



var fruit = ["Banana", "Orange", "Apple", "Mango", "Kiwi"];
var removed = fruit.splice(0, 1);
console.log(removed);
console.log(fruit);




结果为["Orange", "Apple", "Mango", "Kiwi"]

答案 1 :(得分:1)

如果要从数组中获取第一个值从数组中删除它,可以使用Array#shiftMDN | spec) :



var a = [1, 2, 3, 4];   // An array
var first = a.shift();  // Remove and return the first entry
console.log(first);     // Show the value we got
console.log(a);         // Show the updated state of the array