如何从数组中删除一个特定值? 类似 :
array.remove(value);
我必须使用核心核心核心JavaScript。 不允许框架 。
如何从数组中删除一个特定值? 类似 :
array.remove(value);
我必须使用核心核心核心JavaScript。 不允许框架 。
当前回答
Array.prototype.removeByValue = function (val) {
for (var i = 0; i < this.length; i++) {
if (this[i] === val) {
this.splice(i, 1);
i--;
}
}
return this;
}
var fruits = ['apple', 'banana', 'carrot', 'orange'];
fruits.removeByValue('banana');
console.log(fruits);
// -> ['apple', 'carrot', 'orange']
其他回答
我喜欢这个组合的版本, 以其值来去除元素, 使用$.inArray
:
$(document).ready(function(){
var arr = ["C#","Ruby","PHP","C","C++"];
var itemtoRemove = "PHP";
arr.splice($.inArray(itemtoRemove, arr),1);
});
很简单,只要做:
var arr = [1,2,4,5];
arr.splice(arr.indexOf(5), 1);
console.log(arr); // [1,2,4];
如果您必须支持旧版的 Internet Explorer 版本, 我建议使用以下多填充( 注意: 这是否这是所有现代阵列方法(JavaScript 1.8.5/ECMAScript 5 Array Extras)的100%后向兼容的替代方法,这些方法适用于互联网探索者6+、Firefox 1.5+、Chrome、Safari和Opera。
主要有两种办法:
复数(): anArray.splice(index, 1);
let fruits = ['Apple', 'Banana', 'Mango', 'Orange']
let removed = fruits.splice(2, 1);
// fruits is ['Apple', 'Banana', 'Orange']
// removed is ['Mango']
删除删除: delete anArray[index];
let fruits = ['Apple', 'Banana', 'Mango', 'Orange']
let removed = delete fruits(2);
// fruits is ['Apple', 'Banana', undefined, 'Orange']
// removed is true
使用时要小心delete
用于对数组的数组。它有利于删除对象的属性,但对于数组则不那么好。最好使用splice
用于数组。
请注意,当使用delete
对于一个数组,您可能会获得错误的结果anArray.length
换句话说,delete
将会删除元素, 但它不会更新长度属性的值 。
使用删除后,也可以期望在索引编号上出现空洞,例如,最后可能会有第1、3、4、8、9和11号指数,而之前的长度与使用删除时相同。for
循环会崩溃, 因为索引不再是相继的 。
被迫使用delete
出于某种原因,你应该使用for each
需要通过数组循环时循环循环。事实上,总是避免使用索引for
如果可能的话,循环。这样代码就会更稳健,更不易遇到指数问题。
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;
}