0

How to remove object/array from javascript variable?

2
  • 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. Commented Apr 29, 2015 at 16:26
  • possible duplicate of Remove last item from array Commented Apr 29, 2015 at 16:43

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

If I want to remove 3rd array then how will I do it?
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.
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.

3 Comments

If I want to remove 3rd array then how will I do it?
Added to the answer how to remove any element from an array
: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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.