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:-
- array.splice(2, 1);
- array.splice(3, 1);
But Can I achieve this in a single step without using splice()
method twice?
If you know the exact values you want removed you can use array.filter()
.
This removes more than one element from an array at different indexes at same time.
Example in your case:
var filtered = [1,2,3,4,5].filter(value => { return value !== 3 && value !== 5 }); console.log(filtered)
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 => !(np)) && 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)`
Hard code when you know the specific values you need to remove and when the array is small:
var array = [1,2,3,4,5,6,7]; array.splice(2,3,4); console.log(array);
May be you can do like this in place;
var arr = [1,2,3,4,5,17,23,37,61,15], itemsToGetRidOff = [3,17,15,1,23]; arr.reduceRight((_,e,i,a) => itemsToGetRidOff.includes(e) && a.splice(i,1),[]); console.log(arr);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.