iteasy
iteasy

Reputation: 31

get wrong output when remove item from array in javascript

I want remove one item from my array. For example i want remove "Apple" from array. The code like below :

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2,1);

When i run this code, the output is ["Apple"]. The output should be ["Banana", "Orange", "Mango"]. Please help me to find what wrong with my code?

Upvotes: 0

Views: 69

Answers (2)

MFaouzi
MFaouzi

Reputation: 1

The splice() method adds/removes items to/from an array, and returns the removed item(s). This link could help you: https://www.w3schools.com/jsref/jsref_splice.asp

Upvotes: 0

Rahul
Rahul

Reputation: 18557

I am not sure, but yes, you must be taking extract from that splice, instead you need to take actual array as is.

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2,1);
console.log(fruits);

Upvotes: 1

Related Questions