[英]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. 我有一个整数数组,我正在使用
.push()
方法添加。
I know that splice()
method adds/removes items to/from an array, and returns the removed item(s). 我知道
splice()
方法在数组中添加/删除项目,并返回删除的项目。
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. 我知道我们也可以使用
filter()
方法但filter()
方法创建一个新数组,其中包含所有传递由我提供的函数实现的测试的元素,这是我不想要的。
Ex:- 例如: -
var array = [1,2,3,4,5];
I know to remove 3 and 5 I can follow following steps:- 我知道删除3和5我可以按照以下步骤操作: -
- array.splice(2, 1);
array.splice(2,1);
- array.splice(3, 1);
array.splice(3,1);
But Can I achieve this in a single step without using splice()
method twice? 但是,如果不使用
splice()
方法两次,我可以一步完成吗?
If you know the exact values you want removed you can use array.filter()
. 如果您知道要删除的确切值,则可以使用
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 您还可以使用
.forEach()
迭代一个元素数组来匹配, .indexOf()
.splice()
来删除与数组中元素值匹配的元素
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()
使用
for..of
循环, 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()
使用
for..of
循环与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 你也可以使用
for
循环, Object.assign()
来设置索引以保持在数组的开头,调用.splice()
并将参数设置为-
.length
要从数组中删除的元素数量
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()
将
Array.prototype.reduce()
与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 另一个选项,如果数字是唯一的,则使用
Set
, .delete()
,在解构赋值时使用rest元素将Set
对象转换为Array
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.