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

array.remove(value);

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


当前回答

var index,
    input = [1,2,3],
    indexToRemove = 1;
    integers = [];

for (index in input) {
    if (input.hasOwnProperty(index)) {
        if (index !== indexToRemove) {
            integers.push(result); 
        }
    }
}
input = integers;

此解决方案将需要一系列输入, 并将通过输入查找要删除的值。 这将在整个输入数组中循环, 结果将是第二个已经删除了特定索引的数组整数组。 然后将整数组复制到输入数组中 。

其他回答

var index,
    input = [1,2,3],
    indexToRemove = 1;
    integers = [];

for (index in input) {
    if (input.hasOwnProperty(index)) {
        if (index !== indexToRemove) {
            integers.push(result); 
        }
    }
}
input = integers;

此解决方案将需要一系列输入, 并将通过输入查找要删除的值。 这将在整个输入数组中循环, 结果将是第二个已经删除了特定索引的数组整数组。 然后将整数组复制到输入数组中 。

我建议删除一个使用删除和过滤的阵列项目:

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] 控制台。

因此,我们只能删除一个特定的数组项目,而不是所有具有相同价值的项目。

正在删除带有索引和相交点的值 !

function removeArrValue(arr,value) {
    var index = arr.indexOf(value);
    if (index > -1) {
        arr.splice(index, 1);
    }
    return arr;
}

检查一下这个代码 每个主要浏览器都使用这个代码

删除项目 = 函数( rr) { varb = ' ; 对于 (b in arr) { 如果 (ar[ b] { { { { { rr. splice( b, 1); 折断 ; } rr; } 返回 arr; } } 返回 arr; } ; var 数组 = [1, 5,6, 9, 5, 5, 355] var res = mote_ item( arry, 5); 控制台. log( res) = [1, 3, 5, 9, 5, 5, 55, Var Res = dection_ item( arry, 5); 控制台 log( res)

我对基底 JavaScript 阵列进行了相当高效的扩展:

Array.prototype.drop = function(k) {
  var valueIndex = this.indexOf(k);
  while(valueIndex > -1) {
    this.removeAt(valueIndex);
    valueIndex = this.indexOf(k);
  }
};