如何从数组中删除一个特定值? 类似 :

array.remove(value);

制约:我必须使用核心 JavaScript 。 框架不允许 。


当前回答

除了所有这些解决方案之外, 它也可以用阵列来完成. 减量...

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]

其他回答

您在数组中有 1 到 9 个, 您想要删除 5 个 。 请使用以下代码 :

var number Array = [1、2、3、4、5、6、7、8、9]; var new numberArray = number Array. filter (m { return m!= 5;}); 控制台.log ("新阵列,5 develop",新的数字阵列);


如果您想要多个值。例如:- 1,7,8

var number Array = [1、2、3、4、5、6、7、8、9]; var new numberArray = number Array. filter (m {{{{ {返回} (m = = == 1) {(m = = = 7) {(m = = 7) { (m = = = = = 7;} 控制台.log ("新阵列,1,7和8 移除",新的数字阵列);


如果您想要删除数组中的数组值。 例如 : [3,4,5]

var number Array = [1、2、3、4、5、6、7、8、9]; var remoble Array = [3、4、5、5、6、7、8、9]; var new number Array = number Array. filter (m { { return $remobleAry. includs(m) } 控制台.log ("新阵列, [3、4、5] 移除" , 新的数字阵列);

包括支持的浏览器为链接 。

 const Delete = (id) => {
      console.log(id)
     var index = Array.map(function(e){
      return e.id;
     
     }).indexOf(id);
     Array.splice(index,1);
     
}

最干净的:

var arr = ['1','2','3']; arr = arr.filter (e'e? e?=======================================================================================================================================================3'3'); 控制台.warn(arr);

这也将删除重复(如果有的话)。

通过传递其值来删除项目 --

const remove=(value)=>{
    myArray = myArray.filter(element=>element !=value);

}

将项目通过索引编号删除 -

  const removeFrom=(index)=>{
    myArray = myArray.filter((_, i)=>{
        return i!==index
    })
}

您可以从数组中添加一个原型函数来“ 移除” 元素 。

以下示例显示当我们知道一个元素的索引时, 如何简单地从数组中删除一个元素。 我们用它来使用 Array. filter 方法 。

Array. prototype. removeByIndex = 函数 (i) {如果 (! Number. is Integer(i) {i < 0) {/ i 必须是一个整数返回它;} 返回此. filter( f, indx) { indx! = i)} var a = [5, - 89, 2 * 2, " some string", null, froid, 未定义, 20, null, 5]; var b = a. remove ByIndex(2); control.log(a); control.log(b);

有时候我们不知道元素的索引

Array. prototype. remove = 函数 (i) { 返回此. filter (f \ \ f \ f y = i) {var a = [5, - 89, 2 ( 2 * 2), “ 一些字符串 ” 无效, 错误, 未定义, 20, 无效, 5] ; var b = a. remove(5). remove (null); 控制台. log (a); 控制台. log (b) / 它消除了所有搜索值的发生次数 。

但是,当我们只想要删除搜索值的首次出现时, 我们可以在函数中使用 Array. indexof 方法 。

Array. prototype. removeFirst = 函数 (i) {i = this.indexof (i); if (! Number. is Integer (i) {i < 0) {返回此 ;} 返回此. filter (f, indx) { indx = indx ! = i)} var a = [5, - 89, 2 * 2, " some string", null, fraud, 未定义, 20, null, 5]; var b = a.remove First(5).remove First(nell); translavor.log(a) ; controad.log(b);