简体   繁体   English

如何拆分JavaScript数组?

[英]How to split a javascript array?

In python, I can remove the first item from array xs like so: 在python中,我可以像这样从数组xs删除第一项:

xs = xs[1:]

How could I do that with the following javascript array? 如何使用以下JavaScript数组来做到这一点?

var data = {{data}}

The JavaScript splice function, not an specific of jQuery. JavaScript splice功能,而不是jQuery的特定功能。

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

The result is ["Orange", "Apple", "Mango", "Kiwi"] 结果为["Orange", "Apple", "Mango", "Kiwi"]

If you want to get the first value out of an array and remove it from the array, you can use Array#shift ( MDN | spec ): 如果你想获得的第一个值从数组,并从数组中删除它,你可以使用Array#shiftMDN | 规格 ):

 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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM