0

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?

4
  • 1
    output is ["Banana", "Orange", "Mango"], check the value of fruits. You might be looking at the return value of splice. Commented Dec 27, 2017 at 10:39
  • splice returns the plucked element from the array, check the array itself and it would have been modified. Commented Dec 27, 2017 at 10:41
  • developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Dec 27, 2017 at 10:43
  • The output is ["Apple"] because i try to console.log(fruits.splice(2,1)) and like all you say it return value of splice not the value of array. After that i try console('fruits') and it's return ["Banana", "Orange", "Mango"]. Thanks for the solution @gurvinder372 @js_noob Commented Dec 28, 2017 at 4:56

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your solution. I use console.log(fruits.splice(2,1)) with expected the result of array. But now i know if it just return the value of splice. Once again thanks for you contribution :)
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.