如何从数组中删除一个特定值? 类似 :
array.remove(value);
制约:我必须使用核心 JavaScript 。 框架不允许 。
如何从数组中删除一个特定值? 类似 :
array.remove(value);
制约:我必须使用核心 JavaScript 。 框架不允许 。
当前回答
我建议删除一个使用删除和过滤的阵列项目:
var arr = [1,2,3,4,5,5,5,6,7,8,9];删除ar[5];rr = arr.filter(职能(项目){返回项目!=未定义;});//结果:[1、2,3,4,5,6,6,7,7,8,9];删除ar[5];ar = arr.filter(职能(项目){返回项目);//结果:[1、2,3,4,6,6,6,7,7,8,9] 控制台。
因此,我们只能删除一个特定的数组项目,而不是所有具有相同价值的项目。
其他回答
除了所有这些解决方案之外, 它也可以用阵列来完成. 减量...
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( 位置) ;
Array. prototype. remove = 函数 (pos) {此. splice(pos, 1);} var arr = ["a", "b", "c", "d", "e"]; arr. remove(2); // 移除"c" 控制台.log(ar);
如果您不知道要删除的项目的位置, 请使用这个 :
array.erase(element);
Array. prototype. erase = 函数( el) { let p = this. indexof (el); // indexof use prettical equality () { this. spliice (p, 1);}} var ar ar = ["a", "b", "c", "d", "e"; arr.erase (c"); 控制台. log (a) ;
我喜欢这个单行道:
arr.includes(val) && arr.splice(arr.indexOf(val), 1)
ES6 (无互联网探索者支持) 清除已就位 。 快速 : 没有重复重复或重复 。 支持删除无定义或未定义的值
作为原型
// remove by value. return true if value found and removed, false otherwise
Array.prototype.remove = function(val)
{
return this.includes(val) && !!this.splice(this.indexOf(val), 1);
}
(是的,我读了所有其他的答案 找不到一个结合的答案 包含和串联在同一行。 )
大部分答案都用...
循环的索引和组合删除过滤器
虽然所有解决方案都应该采用这些方法,但我想我们可以使用弦操纵。
需要注意的关于这一解决办法的要点 -- --
这将在数据中留下漏洞( 可以通过额外的过滤器将其移除 ) 此解决方案不仅适用于原始搜索值, 也适用于对象 。
诀窍是...
字符串字符串返回分隔符上的分解数据,以空字符串替换输入数据集中的搜索值。
remove = (input, value) => {
const stringVal = JSON.stringify(value);
const result = JSON.stringify(input)
return result.replace(stringVal, "").split(",");
}
本文创建了一个测试对象和数字的 JSF 中心- https://jsfidle.net/4t7zhkce/33/
检查小提琴中的移除方法 。
使用.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 (“ 原件饮料阵列 :” +饮料 );