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

array.remove(value);

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


当前回答

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

arr.splice(0,1)

console.log(arr)

产出[2、3、4、5];

其他回答

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

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

开始

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

删除“ 计算” 选项@ action

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

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

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

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

如需更多参考,请通过:

Array. prototype.spice ()

答案已经很多了, 但是因为还没有人用一个衬里来做, 我想我会展示我的方法。 它会利用字符串. split () 函数在创建数组时将删除所有指定字符这一事实。 这里举一个例子 :

var ary = [1、2、3、4、1234、10、4、5、7、3]; out = arry.join (" -" -").split ("-4 -").join (" -").split (" -").split (" -");control.log(out) ;

在此示例中, 所有 4 个的字符都在从数组中移除 。 但是, 必须指出, 包含字符“ - ” 的任何数组都会与此示例产生问题 。 简而言之, 这会导致组合( “ - ” ) 函数不适当地将您的字符串拼凑在一起。 在这种情况下, 上面的扇形中的所有“ - ” 字符串都可以替换为在原始数组中不会使用的任何字符串 。 以下还有一个示例 :

var ary = [1,2,3,4,'-',1234,10,'-',4,5,7,3]; out = ary.join("!@#").split("!@#4!@#").join("!@#").split("!@#"); console.log(out);

使用 JavaScript 原型特性定义列对象上名为删除() 的方法 。

使用 spolice () 方法满足您的要求 。

请看看下面的代码

Array.prototype.remove = function(item) {
    // 'index' will have -1 if 'item' does not exist,
    // else it will have the index of the first item found in the array
    var index = this.indexOf(item);

    if (index > -1) {
        // The splice() method is used to add/remove items(s) in the array
        this.splice(index, 1);
    }
    return index;
}

var arr = [ 11, 22, 67, 45, 61, 89, 34, 12, 7, 8, 3, -1, -4];

// Printing array
// [ 11, 22, 67, 45, 61, 89, 34, 12, 7, 8, 3, -1, -4];
console.log(arr)

// Removing 67 (getting its index, i.e. 2)
console.log("Removing 67")
var index = arr.remove(67)

if (index > 0){
    console.log("Item 67 found at ", index)
} else {
    console.log("Item 67 does not exist in array")
}

// Printing updated array
// [ 11, 22, 45, 61, 89, 34, 12, 7, 8, 3, -1, -4];
console.log(arr)

// ............... Output ................................
// [ 11, 22, 67, 45, 61, 89, 34, 12, 7, 8, 3, -1, -4 ]
// Removing 67
// Item 67 found at  2
// [ 11, 22, 45, 61, 89, 34, 12, 7, 8, 3, -1, -4 ]

注:以下是Norde.js REPL上执行的完整示例代码,该代码描述推()、流行()、转移()、非轮档()和交点()方法的使用。

> // Defining an array
undefined
> var arr = [12, 45, 67, 89, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34];
undefined
> // Getting length of array
undefined
> arr.length;
16
> // Adding 1 more item at the end i.e. pushing an item
undefined
> arr.push(55);
17
> arr
[ 12, 45, 67, 89, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34, 55 ]
> // Popping item from array (i.e. from end)
undefined
> arr.pop()
55
> arr
[ 12, 45, 67, 89, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34 ]
> // Remove item from beginning
undefined
> arr.shift()
12
> arr
[ 45, 67, 89, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34 ]
> // Add item(s) at beginning
undefined
> arr.unshift(67); // Add 67 at beginning of the array and return number of items in updated/new array
16
> arr
[ 67, 45, 67, 89, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34 ]
> arr.unshift(11, 22); // Adding 2 more items at the beginning of array
18
> arr
[ 11, 22, 67, 45, 67, 89, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34 ]
>
> // Define a method on array (temporarily) to remove an item and return the index of removed item; if it is found else return -1
undefined
> Array.prototype.remove = function(item) {
... var index = this.indexOf(item);
... if (index > -1) {
..... this.splice(index, 1); // splice() method is used to add/remove items in array
..... }
... return index;
... }
[Function]
>
> arr
[ 11, 22, 67, 45, 67, 89, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34 ]
>
> arr.remove(45);    // Remove 45 (you will get the index of removed item)
3
> arr
[ 11, 22, 67, 67, 89, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34 ]
>
> arr.remove(22)    // Remove 22
1
> arr
[ 11, 67, 67, 89, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34 ]
> arr.remove(67)    // Remove 67
1
> arr
[ 11, 67, 89, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34 ]
>
> arr.remove(89)    // Remove 89
2
> arr
[ 11, 67, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34 ]
>
> arr.remove(100);  // 100 doesn't exist, remove() will return -1
-1
>

您可以使用ES6. 例如,在此情况下删除值“ 3” :

var array=['1','2','3','4','5','6']
var newArray = array.filter((value)=>value!='3');
console.log(newArray);

产出:

["1", "2", "4", "5", "6"]

您在数组中有 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] 移除" , 新的数字阵列);

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