Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove a range of elements from an array

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?

like image 234
Run Avatar asked Sep 13 '17 13:09

Run


People also ask

How do you delete a range of elements in an array Java?

Answer: Java does not provide a direct method to remove an element from the array. But given an index at which the element is to be deleted, we can use ArrayList to remove the element at the specified index. For this, first, we convert the array to ArrayList and using the remove method we remove the element.

How do you delete multiple elements from an array?

Approach 1: Store the index of array elements into another array which need to be removed. Start a loop and run it to the number of elements in the array. Use splice() method to remove the element at a particular index.

Can you remove elements from an array in C#?

To delete an elements from a C# array, we will shift the elements from the position the user want the element to delete.

Can we delete elements from array?

To delete a specific element from an array, a user must define the position from which the array's element should be removed. The deletion of the element does not affect the size of an array. Furthermore, we should also check whether the deletion is possible or not in an array.


2 Answers

The second param of Array.prototype.splice() method 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 how should be your code:

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);
like image 119
cнŝdk Avatar answered Oct 09 '22 18:10

cнŝdk


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

like image 40
Dream_Cap Avatar answered Oct 09 '22 19:10

Dream_Cap