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

array.remove(value);

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


当前回答

尝试使用删除运算符删除数组元素

例如:

const arr = [10, 20, 30, 40, 50, 60];
delete arr[2]; // It will Delete element present at index 2
console.log( arr ); // [10, 20, undefined , 40, 50, 60]

注意: 使用删除运算符将会在数组中留下空空空/ 空洞。 它不会提醒数组的长度。 要在元素被删除时更改数组的长度, 请使用复数法 。

希望这能解决你所有的问题

其他回答

2017-005-08

大多数给定的回答都用于严格的比较, 意思是两个对象在内存( 或原始类型) 中引用完全相同的对象, 但通常您想要从具有一定值的数组中删除一个非原始对象。 例如, 如果您给服务器打电话, 并想要对照本地对象检查已检索到的对象 。

const a = {'field': 2} // Non-primitive object
const b = {'field': 2} // Non-primitive object with same value
const c = a            // Non-primitive object that reference the same object as "a"

assert(a !== b) // Don't reference the same item, but have same value
assert(a === c) // Do reference the same item, and have same value (naturally)

//Note: there are many alternative implementations for valuesAreEqual
function valuesAreEqual (x, y) {
   return  JSON.stringify(x) === JSON.stringify(y)
}


//filter will delete false values
//Thus, we want to return "false" if the item
// we want to delete is equal to the item in the array
function removeFromArray(arr, toDelete){
    return arr.filter(target => {return !valuesAreEqual(toDelete, target)})
}

const exampleArray = [a, b, b, c, a, {'field': 2}, {'field': 90}];
const resultArray = removeFromArray(exampleArray, a);

//resultArray = [{'field':90}]

数值AreEqual有替代/更快的操作,但这样可以操作。如果您有特定的字段要检查,也可以使用自定义的比较器(例如,有些已检索的 UUID 相对于本地的 UUID ) 。

2. 还注意到这是一个功能操作,意即它不改变原始阵列。

OK,例如,您有下面的数组:

var num = [1, 2, 3, 4, 5];

我们想要删除第4号, 你可以简单地使用下面的代码:

num.splice(num.indexOf(4), 1); // num will be [1, 2, 3, 5];

如果您正在重复使用此函数,请写入一个可重复使用的函数,该函数将附加在本地数组函数上,如下文所示:

Array.prototype.remove = Array.prototype.remove || function(x) {
  const i = this.indexOf(x);
  if(i===-1)
      return;
  this.splice(i, 1); // num.remove(5) === [1, 2, 3];
}

但如果您有下面的数组, 而不是数组中的几个 [5] 呢?

var num = [5, 6, 5, 4, 5, 1, 5];

我们需要一个循环来检查它们, 但是一个更容易和更有效的方法是使用内置的 JavaScript 函数, 所以我们写一个函数, 使用下面这样的过滤器 :

const _removeValue = (arr, x) => arr.filter(n => n!==x);
//_removeValue([1, 2, 3, 4, 5, 5, 6, 5], 5) // Return [1, 2, 3, 4, 6]

还有第三方图书馆,如Lodash 或Goint, 也帮助你这样做。更多信息,请参看 Lodash _. pull,_. pullAt 或_。

使用过滤器是实现此要求的优雅方式。 过滤器不会改变原始数组 。

const num = 3; 允许 rr = [1, 2, 3, 4]; const arr2 = arr. filter(x xx x x !x != num); 控制台. log (rr); / / [1, 2, 3, 4] 控制台. log(rr2) / / [1, 2, 4]

如果您想要实现突变删除行为, 您可以使用过滤器, 然后将结果指定到原始数组 。

constnum = 3; let arr = [1, 2, 3, 4]; arr = arr. filter(x x \ x = = = = = = = = = = = = = = = = = =; 控制台. log(rr); / / [1, 2, 4]

顺便说一下, 过滤器将删除在条件中匹配的所有事件( 不仅仅是第一次发生) , 就像您在下面的例子中看到的一样 。

const num = 3; 允许 rr = [1, 2, 3, 3, 3, 3, 4]; arr = arr. filter(x x x x x = = = = = = = = num; 控制台. log (rr); / / [1, 2, 4]

万一您只想删除第一种情况,您可以使用复数法

onst num = 3; 让 arr = [1, 2, 3, 3, 3, 3, 4]; const idx = rr. indexof( num); arr.spice( idx, idx) = = 1 = 1 = 1 = 1 ; 控制台. log( rr) = [ 1, 2, 3, 3, 3, 4] / [ 1, 3, 3, 4]

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

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]

组合法通过删除或替换现有元素和/或添加新元素来改变数组的内容。

数组。 spice( 开始 [, 删除 [, 删除 [, 项1 [, 项2 [, . ] ] ] ]

开始

开始更改数组的索引( 与源值为 0 ) 。 如果数组的长度大于数组的长度, 则实际起始指数将设置为数组的长度。 如果为负, 则开始从数组结尾处( 与源值为-1) 的许多元素, 如果绝对值大于数组的长度, 则将设置为 0 。

删除“ 计算” 选项@ action

表示要删除的旧数组元素数的整数。

如果删除“计算”被省略,或者如果其值大于数组。 长度 - 开始( 即如果它大于数组中剩下的元素数量, 从开始) , 那么从开始到数组结尾的所有元素都将删除。 如果删除“ 计算” 0 或“ 负” , 则不删除任何元素。 在这种情况下, 您应该至少指定一个新元素( 见下文) 。

第1项,第2项,...可选

要添加到数组的元素, 从起始索引开始。 如果您不指定任何元素, 组合() 只会从数组中删除元素 。

如需更多参考,请通过:

Array. prototype.spice ()