7

I want to remove a range of elements from an array:

var fruits = ["Banana", "Orange1", "Apple", "Banana", "Orange", "Banana", "Orange", "Mango", "Bananax", "Orangex"];
var a = fruits.indexOf("Apple");
var b = fruits.indexOf("Mango");

var removedCars = fruits.splice(a, b);

console.log(fruits);

So I am expecting:

["Banana", "Orange1", "Bananax", "Orangex"]

But the result is:

["Banana", "Orange1", "Orangex"]

Why is this happening?

Are there any faster and better ways of doing this?

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
Run
  • 54,938
  • 169
  • 450
  • 748
  • 5
    You are not using the splice function correctly. The first parameter defines number of items, while the second is the start index. – Leth Sep 13 '17 at 13:20
  • 7
    [`array.splice(start, deleteCount)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice) That second parameter isn't an index. – Cerbrus Sep 13 '17 at 13:20

3 Answers3

16

The second param of Array.prototype.splice() is the number of elements to be removed and not an ending index.

You can see from the Array.prototype.splice() MDN Reference that:

Parameters

start Index at which to start changing the array (with origin 0). If greater than the length of the array, actual starting index will be set to the length of the array. If negative, will begin that many elements from the end of the array (with origin 1) and will be set to 0 if absolute value is greater than the length of the array.

deleteCount Optional An integer indicating the number of old array elements to remove. If deleteCount is 0, no elements are removed. In this case, you should specify at least one new element. If deleteCount is greater than the number of elements left in the array starting at start, then all of the elements through the end of the array will be deleted.

Solution:

You need to calculate the number of elements between these two indexes, so use (b - a) + 1 to get the correct count.

Demo:

This is your code with the proper usage of splice:

var fruits = ["Banana", "Orange1", "Apple", "Banana", "Orange", "Banana", "Orange", "Mango", "Bananax", "Orangex"];
var a = fruits.indexOf("Apple");
var b = fruits.indexOf("Mango");

var removedFruits = fruits.splice(a, (b - a) + 1);

console.log(fruits);
Andrew
  • 132
  • 1
  • 9
cнŝdk
  • 31,391
  • 7
  • 56
  • 78
3

Here's one way to do it with filter:

var fruits = ["Banana", "Orange1", "Apple", "Banana", "Orange", "Banana", "Orange", "Mango", "Bananax", "Orangex"];
var a = fruits.indexOf("Apple");
var b = fruits.indexOf("Mango");

//returns items not equal to a or b
function fruitfilter(item, index){
return index !== a && index !== b;
}

//apply the filter to the array, returns a new array
var newfruits = fruits.filter(fruitfilter);
 //log the new fruits
console.log(newfruits);

Here's a jsfiddle: link

Dream_Cap
  • 2,292
  • 2
  • 18
  • 30
0

That happens because array.splice() take the first index and the number of elements you want to delete not the last index try that

fruits.splice(a, b - a + 1);
Mohamed hesham
  • 130
  • 1
  • 12