如何从数组中删除一个特定值? 类似 :
array.remove(value);
我必须使用核心核心核心JavaScript。 不允许框架 。
如何从数组中删除一个特定值? 类似 :
array.remove(value);
我必须使用核心核心核心JavaScript。 不允许框架 。
当前回答
删除某一要素或随后的元素,Array.spolice ()方法运作良好。
组合法通过删除或替换现有元素和(或)添加新元素来改变数组的内容,并返回被删除的项目。
语法: 数组. spice( index, deleteCount, items 1,...,..., itemsX)
给index
休息辩论是强制性的,休息辩论是任择的。
例如:
let arr = [1, 2, 3, 4, 5, 6];
arr.splice(2,1);
console.log(arr);
// [1, 2, 4, 5, 6]
注: Array.spolice ()如果您知道要删除的元素的索引, 可以使用该方法。 但我们可能还会有以下几个例子:
如果您想要删除仅最后一个元素, 您可以使用Array.pop ()
如果您想要删除仅第一个元素, 您可以使用矩阵. trip ()
如果您只知道元素, 而不知道元素的位置(或索引), 并且想要删除所有匹配元素Array. filter ()方法 :
let arr = [1, 2, 1, 3, 4, 1, 5, 1];
let newArr = arr.filter(function(val){
return val !== 1;
});
//newArr => [2, 3, 4, 5]
或者通过使用复数()方法作为:
let arr = [1, 11, 2, 11, 3, 4, 5, 11, 6, 11];
for (let i = 0; i < arr.length-1; i++) {
if ( arr[i] === 11) {
arr.splice(i, 1);
}
}
console.log(arr);
// [1, 2, 3, 4, 5, 6]
或者假设我们想要删除del
从数组arr
:
let arr = [1, 2, 3, 4, 5, 6];
let del = 4;
if (arr.indexOf(4) >= 0) {
arr.splice(arr.indexOf(4), 1)
}
或
let del = 4;
for(var i = arr.length - 1; i >= 0; i--) {
if(arr[i] === del) {
arr.splice(i, 1);
}
}
如果您只知道元素, 而不知道元素的位置( 或索引) , 并且想要使用 复数 () 方法删除第一个匹配元素 :
let arr = [1, 11, 2, 11, 3, 4, 5, 11, 6, 11];
for (let i = 0; i < arr.length-1; i++) {
if ( arr[i] === 11) {
arr.splice(i, 1);
break;
}
}
console.log(arr);
// [1, 11, 2, 11, 3, 4, 5, 11, 6, 11]
其他回答
检查一下这个代码 每个代码都有效主浏览器.
remove_item = function(arr, value) {
var b = '';
for (b in arr) {
if (arr[b] === value) {
arr.splice(b, 1);
break;
}
}
return arr;
};
var array = [1,3,5,6,5,9,5,3,55]
var res = remove_item(array,5);
console.log(res)
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;
}
let array = [5,5,4,4,2,3,4]
let newArray = array.join(',').replace('5','').split(',')
如果您想要删除一个当前项目, 此示例将有效 。
非就地解决办法
arr.slice(0,i).concat(arr.slice(i+1));
let arr = [10, 20, 30, 40, 50]
let i = 2 ; // position to remove (starting from 0)
let r = arr.slice(0,i).concat(arr.slice(i+1));
console.log(r);
这取决于你是否想保持一个空位。
如果您想要一个空位 :
array[index] = undefined;
如果你不想空空的位子:
保留原件:
oldArray = [...array];
这将修改数组。
array.splice(index, 1);
如果您需要该项的值, 您可以保存返回的数组元素 :
var value = array.splice(index, 1)[0];
如果您想要在数组的两端删除, 您可以在最后一个或数组中使用数组. pop () 。 第一个或数组中可以使用数组. translate. pop () 。 translate () toft () for first one (两者都返回项目值 ) 。
如果您不知道项目的索引, 您可以使用数组。 indexof( 项) 来获得它( 在 if () 中获得一个项, 或者在一段时间( ) 中获取全部项 ) 。array.indexOf(item)
如果找不到的话,返回索引或 -1。