How to remove object/array from javascript variable?
Asked
Active
Viewed 84 times
0
-
Remember to format the code in your question using the charachter ` or selecting the code and hitting the "format code" button. Anyway is not really important to post all the data... you could've just asked how to delete the last item in an array. – Bolza Apr 29 '15 at 16:26
-
possible duplicate of [Remove last item from array](http://stackoverflow.com/questions/19544452/remove-last-item-from-array) – Jason Cust Apr 29 '15 at 16:43
2 Answers
4
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);

MrCode
- 63,975
- 10
- 90
- 112
-
-
If the array position is not fixed, then how you remove the array ? I mean not using index/position. May be by using for loop. – CodeMachine May 03 '15 at 09:21
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.

Bolza
- 1,904
- 2
- 17
- 42
-
-
-
:If the array position is not fixed, then how you remove the array ? I mean not using index/position. May be by using for loop. – CodeMachine May 03 '15 at 09:20