Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove more than one elements from an array from different indexes at same time in JavaScript?

I have an array of integers, which I'm using the .push() method to add to.

I know that splice() method adds/removes items to/from an array, and returns the removed item(s).

But is there is a way to remove multiple elements from the array at different indexes at the same time?

I know we can also use filter() method but The filter() method creates a new array with all elements that pass the test implemented by the provided function which I don't want.

Ex:-

var array = [1,2,3,4,5];

I know to remove 3 and 5 I can follow following steps:-

  1. array.splice(2, 1);
  2. array.splice(3, 1);

But Can I achieve this in a single step without using splice() method twice?

like image 297
Gaurav joshi Avatar asked Mar 10 '23 19:03

Gaurav joshi


1 Answers

You can use trailing comma at destructuring assignment to select specific elements from array, assign resulting values within array to original array reference.

var array = [1,2,3,4,5];
{let [a,b,,c,,] = array; array = [a,b,c]};

console.log(array);

You can use object destructuring on an array to get only specific indexes from array

var array = [1,2,3,4,5];
{let {0:a,1:b,3:c} = array; array = [a,b,c]};

console.log(array);

You can also use .forEach() to iterate an array of elements to match, .indexOf(), .splice() to remove elements which match value of element within array

var array = [1,2,3,4,5];
[3, 5].forEach(p => array.splice(array.indexOf(p), 1));

console.log(array);

Using for..of loop, Array.prototype.findIndex(), Array.prototype.splice()

var array = [1,2,3,4,5];
var not = [3, 5];
for (let n of not) array.splice(array.findIndex(v => v === n), 1);

console.log(array);

Using for..of loop with Array.prototype.entries(), Array.prototype.some(), Array.prototype.splice()

var array = [1,2,3,4,5];
var not = [3,5];
for (let [k, p] of array.entries()) not.some(n => !(n-p)) && array.splice(k, 1);

console.log(array);

You can also use for loop, Object.assign() to set indexes to keep at beginning of array, call .splice() with parameter set to - .length of the number of elements to remove from array

var array = [1,2,3,4,5];
var not = [3,5];
for (var o = {}, k = -1, i = 0; i < array.length; i++) {
  if (!not.some(n => n === array[i])) o[++k] = array[i];
}
Object.assign(array, o).splice(-not.length);

console.log(array);

Using Array.prototype.reduce() with Object.assign()

var array = [1,2,3,4,5];
var not = [3,5];
Object.assign(array, array.reduce(([o, not, k], p) => 
  [Object.assign(o, !not.some(n => n === p) ? {[++k]:p} : void 0), not, k]
  , [{}, not, -1]).shift()
).splice(-not.length);

console.log(array);

Another option, if the numbers are unique, is to use Set, .delete(), convert Set object to Array using rest element at destructuring assignment

var array = new Set;
var not = [3, 5];
for (let n = 1; n <= 5; n++) array.add(n);

for (let n of not) array.delete(n);
[...array] = array;

console.log(array); // `array = new Set(array)`
like image 119
guest271314 Avatar answered Apr 26 '23 02:04

guest271314