简体   繁体   中英

How to remove object/array from javascript variable?

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

You can use Array.prototype.pop() to remove the last element of an array.

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

You can use the .pop() method.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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