如何从数组中删除一个特定值? 类似 :
array.remove(value);
制约:我必须使用核心 JavaScript 。 框架不允许 。
如何从数组中删除一个特定值? 类似 :
array.remove(value);
制约:我必须使用核心 JavaScript 。 框架不允许 。
当前回答
我知道已经有很多答案了, 但其中许多答案似乎都使问题复杂化了。 这是一个简单的、循环的删除所有关键实例的方法- 在找不到索引之前自呼自用。 是的, 它只在有索引的浏览器中起作用, 但是它很简单, 并且很容易被多填 。
独立职能
function removeAll(array, key){
var index = array.indexOf(key);
if(index === -1) return;
array.splice(index, 1);
removeAll(array,key);
}
原型方法
Array.prototype.removeAll = function(key){
var index = this.indexOf(key);
if(index === -1) return;
this.splice(index, 1);
this.removeAll(key);
}
其他回答
这提供了一个前提值, 而不是一个值 。
注:它将更新给定阵列,并返回受影响的行。
用法
var removed = helper.remove(arr, row => row.id === 5 );
var removed = helper.removeAll(arr, row => row.name.startsWith('BMW'));
定义定义定义定义定义的定义定义定义定义定义的定义定义定义定义定义的定义定义定义定义定义定义的定义
var helper = {
// Remove and return the first occurrence
remove: function(array, predicate) {
for (var i = 0; i < array.length; i++) {
if (predicate(array[i])) {
return array.splice(i, 1);
}
}
},
// Remove and return all occurrences
removeAll: function(array, predicate) {
var removed = [];
for (var i = 0; i < array.length; ) {
if (predicate(array[i])) {
removed.push(array.splice(i, 1));
continue;
}
i++;
}
return removed;
},
};
使用.indexof () 和.spolice () - 可变模式
这里有两种情况:
我们知道指数
康斯特饮料 = [“Tea”、“咖啡 ”、“Milk ”]; 康斯特 id = 1; 康斯特 删除饮料 = 饮料.splice(id, 1); 控制台.log (removedDrink)
我们不知道指数,但知道它的价值。 Const drinks = [“Tea”,“Coffee”,“Milk”]; const id = drinks.indexof (“Coffee”);// 1 Const moved Drink = drink.spice(id,1);// / [“Coffee”] 控制台.log(removedDrink);// [“Tea”,“Milk” 控制台.log(drinks);
使用. filter () - 不可变模式
你可以想到的最好方式是 — — 而不是“删除”该项目,而是“创造”一个新的阵列,只是不包含该项目。 所以我们必须找到它,完全省略它。
康斯特饮料 = [“Tea”、“Coffee”、“Milk ”]; Const id = “Coffee”; Const idx = drinks.indexof(id); 康斯特 移除的Drink = 饮料 [idx]; 康斯特过滤的饮料 = 饮料 ; Const 过滤的Drinks = 饮料 . filter ((饮料,索引) 饮料 = 去除的Drink); 控制室.log (“ Filter 饮料阵列 :”+过滤的饮料 ); 控制室.log (“ 原件饮料阵列 :” +饮料 );
除了所有这些解决方案之外, 它也可以用阵列来完成. 减量...
const removeItem =
idx =>
arr =>
arr.reduce((acc, a, i) => idx === i ? acc : acc.concat(a), [])
const array = [1, 2, 3]
const index = 1
const newArray = removeItem(index)(array)
console.log(newArray) // logs the following array to the console : [1, 3]
...或者一个循环函数(诚实地说不是那么优雅...也许有人有更好的循环解决方案? ? )...
const removeItemPrep =
acc =>
i =>
idx =>
arr =>
// If the index equals i, just feed in the unchanged accumulator(acc) else...
i === idx ? removeItemPrep(acc)(i + 1)(idx)(arr) :
// If the array length + 1 of the accumulator is smaller than the array length of the original array concatenate the array element at index i else...
acc.length + 1 < arr.length ? removeItemPrep(acc.concat(arr[i]))(i + 1)(idx)(arr) :
// return the accumulator
acc
const removeItem = removeItemPrep([])(0)
const array = [1, 2, 3]
const index = 1
const newArray = removeItem(index)(array)
console.log(newArray) // logs the following array to the console : [1, 3]
如果您想要 [.].remove(el) 类似语法的语法, 和其他编程语言一样, 那么您可以添加此代码 :
// Add remove method to Array prototype
Array.prototype.remove = function(value, count=this.length) {
while(count > 0 && this.includes(value)) {
this.splice(this.indexOf(value), 1);
count--;
}
return this;
}
用法
// Original array
const arr = [1,2,2,3,2,5,6,7];
// Remove all 2s from array
arr.remove(2); // [1,3,5,6,7]
// Remove one 2 from beginning of array
arr.remove(2, 1); // [1,2,3,2,5,6,7]
// Remove two 2s from beginning of array
arr.remove(2, 2); // [1,3,2,5,6,7]
您可以根据您的需求操控该方法 。
洛达什:
let a1 = {name:'a1'}
let a2 = {name:'a2'}
let a3 = {name:'a3'}
let list = [a1, a2, a3]
_.remove(list, a2)
//list now is [{name: "a1"}, {name: "a3"}]
请选中此项以获取细节 :. remove( 数组, [预测=. identity] )