CodeMachine
CodeMachine

Reputation: 35

How to remove object/array from javascript variable?

How to remove object/array from javascript variable?

Upvotes: 0

Views: 92

Answers (2)

Bolza
Bolza

Reputation: 1944

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.

Upvotes: 1

MrCode
MrCode

Reputation: 64526

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);

Upvotes: 4

Related Questions