如何从数组中删除一个特定值? 类似 :
array.remove(value);
我必须使用核心核心核心JavaScript。 不允许框架 。
如何从数组中删除一个特定值? 类似 :
array.remove(value);
我必须使用核心核心核心JavaScript。 不允许框架 。
当前回答
我想根据ECMAScript 6。假设您拥有如下阵列:
let arr = [1,2,3,4];
如果您想要在特殊索引中删除2
,写下以下代码:
arr.splice(2, 1); //=> arr became [1,2,4]
但是,如果你想删除一个特殊项目,比如3
并且你不知道它的索引, 做如下:
arr = arr.filter(e => e !== 3); //=> arr became [1,2,4]
提示提示:请使用箭头函数进行过滤回击,除非您得到空数组。
其他回答
var array = [2, 5, 9];
var res = array.splice(array.findIndex(x => x==5), 1);
console.log(res)
使用 Array. findindindex, 我们可以减少代码行数。
虽然大多数前一个答复都回答了问题,但现在还不清楚为什么slice()
未使用方法。是的,filter()
符合不可改变标准,但采用以下较短的等值方法如何?
const myArray = [1,2,3,4];
现在让我们说我们应该从阵列中删除第二个元素, 我们可以简单地做到:
const newArray = myArray.slice(0, 1).concat(myArray.slice(2, 4));
// [1,3,4]
以这种方式从一个阵列中删除元素的方式, 今天社区强烈鼓励从一个阵列中删除元素, 因为它的简单和不可改变的性质。 一般而言, 导致突变的方法应该避免。 例如, 鼓励您替换push()
与concat()
和splice()
与slice()
.
var arr =[1,2,3,4,5];
arr.splice(0,1)
console.log(arr)
产出[2、3、4、5];
没必要用indexOf
或splice
。但是,如果只想要删除一个元素的发生,则该元素的性能会更好。
查找并移动( 移动) :
function move(arr, val) {
var j = 0;
for (var i = 0, l = arr.length; i < l; i++) {
if (arr[i] !== val) {
arr[j++] = arr[i];
}
}
arr.length = j;
}
使用使用indexOf
和splice
(指数):
function indexof(arr, val) {
var i;
while ((i = arr.indexOf(val)) != -1) {
arr.splice(i, 1);
}
}
仅使用splice
(恶作剧):
function splice(arr, val) {
for (var i = arr.length; i--;) {
if (arr[i] === val) {
arr.splice(i, 1);
}
}
}
带有 1000 元素的阵列( 平均超过 10,000 次运行) 的节点js 上的运行时间 :
指数指数慢于移动移动即使通过取消要求indexOf
内复盘,它的表现比移动移动.
Remove all occurrences:
move 0.0048 ms
indexof 0.0463 ms
splice 0.0359 ms
Remove first occurrence:
move_one 0.0041 ms
indexof_one 0.0021 ms
Vanilla JavaScript(ES5.1) - (ES5.1) -已经到位版本版本
浏览器支持 :因特网探索者 9或以后(或以后(详细浏览器支持)
/**
* Removes all occurences of the item from the array.
*
* Modifies the array “in place”, i.e. the array passed as an argument
* is modified as opposed to creating a new array. Also returns the modified
* array for your convenience.
*/
function removeInPlace(array, item) {
var foundIndex, fromIndex;
// Look for the item (the item can have multiple indices)
fromIndex = array.length - 1;
foundIndex = array.lastIndexOf(item, fromIndex);
while (foundIndex !== -1) {
// Remove the item (in place)
array.splice(foundIndex, 1);
// Bookkeeping
fromIndex = foundIndex - 1;
foundIndex = array.lastIndexOf(item, fromIndex);
}
// Return the modified array
return array;
}
Vanilla JavaScript(ES5.1) - (ES5.1) -不可变版本版本
浏览器支持: 与原版的香草 JavaScript 相同
/**
* Removes all occurences of the item from the array.
*
* Returns a new array with all the items of the original array except
* the specified item.
*/
function remove(array, item) {
var arrayCopy;
arrayCopy = array.slice();
return removeInPlace(arrayCopy, item);
}
香草ES6 -不可变版本版本
浏览器支持: Chrome 46, 边缘 12, Firefox 16, Opera 37, Safari 8 ()详细浏览器支持)
/**
* Removes all occurences of the item from the array.
*
* Returns a new array with all the items of the original array except
* the specified item.
*/
function remove(array, item) {
// Copy the array
array = [...array];
// Look for the item (the item can have multiple indices)
let fromIndex = array.length - 1;
let foundIndex = array.lastIndexOf(item, fromIndex);
while (foundIndex !== -1) {
// Remove the item by generating a new array without it
array = [
...array.slice(0, foundIndex),
...array.slice(foundIndex + 1),
];
// Bookkeeping
fromIndex = foundIndex - 1;
foundIndex = array.lastIndexOf(item, fromIndex)
}
// Return the new array
return array;
}