0

How to remove object/array from javascript variable?

  • 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 – Jason Cust Apr 29 '15 at 16:43
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);
  • Thanks a lot man. It works. – CodeMachine Apr 29 '15 at 16:31
  • If I want to remove 3rd array then how will I do it? – CodeMachine Apr 29 '15 at 16:35
  • See my edit..... – MrCode Apr 29 '15 at 16:40
  • 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 3 '15 at 9: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.

  • Thanks a lot. :-) – CodeMachine Apr 29 '15 at 16:32
  • If I want to remove 3rd array then how will I do it? – CodeMachine Apr 29 '15 at 16:35
  • Added to the answer how to remove any element from an array – Bolza Apr 29 '15 at 16:40
  • :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 3 '15 at 9:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.