简体   繁体   English

如何从javascript变量中删除对象/数组?

[英]How to remove object/array from javascript variable?

如何从javascript变量中删除对象/数组?

You can use Array.prototype.pop() to remove the last element of an array. 您可以使用Array.prototype.pop()删除数组的最后一个元素。

bankBranchReponse.pop();

To remove an element at a specific index, for example the 3rd element: 要删除特定索引处的元素,例如第三个元素:

var index = 2; // zero based so 2 is the 3rd element
bankBranchReponse.splice(index, 1);

As for W3CSchool http://www.w3schools.com/jsref/jsref_pop.asp 至于W3CSchool http://www.w3schools.com/jsref/jsref_pop.asp

You can use the .pop() method. 您可以使用.pop()方法。

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.pop();

The result of fruits will be: 水果的结果将是:

Banana,Orange,Apple

Remove another element 删除另一个元素

To remove a certain element at an index inside the array you can use the method splice 要删除数组内部索引处的某个元素,可以使用splice方法

So For example 所以举个例子

var myFish = ['angel', 'clown', 'drum', 'surgeon', 'apple'];
// removes 1 element from index 3
removed = myFish.splice(3, 1);

And the result will be 结果将是

myFish is ['angel', 'clown', 'drum', 'apple']
removed is ['surgeon']

Remember that array is zero-indexed so the 3rd element is the element number 2. 请记住,数组是零索引的,因此第三个元素是元素编号2。

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

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