根据值从javascript数组中删除项目

时间:2022-08-26 14:28:35

I have an array in javascript. I need to remove an item from it. I have to iterate over the array and check whether there is a value called 'mastercheck'. If the value is there in array, I have to remove it and get the remaining items. How to do?

我在javascript中有一个数组。我需要从中删除一个项目。我必须迭代数组并检查是否有一个名为'mastercheck'的值。如果值在数组中,我必须删除它并获取剩余的项目。怎么做?

Typically my array consists of value like mastercheck,60154,60155....

通常我的数组包含像mastercheck,60154,60155 ....

3 个解决方案

#1


4  

First use the indexOf method to determine the index of the item with the needed value. Then you can use the splice method to remove the item at found index.

首先使用indexOf方法确定具有所需值的项的索引。然后,您可以使用splice方法删除找到的索引处的项目。

Something like that:

像这样的东西:

var array = ['mastercheck', '60154', '60155'];
var index = array.indexOf('mastercheck'); // get the index
array.splice(index, 1); // remove the item

#2


2  

var arr = ['mastercheck',60154,60155];

for(var i=0;i<arr.length;i++){
    if(arr[i] === 'mastercheck'){
        arr.splice(i,1);
    } 
}

console.log(arr);

#3


1  

Use this code jsFiddle

使用此代码jsFiddle

var arr = ['mastercheck', '60154', '60155'];
var index = arr.indexOf('mastercheck');
arr.splice(index, 1);

#1


4  

First use the indexOf method to determine the index of the item with the needed value. Then you can use the splice method to remove the item at found index.

首先使用indexOf方法确定具有所需值的项的索引。然后,您可以使用splice方法删除找到的索引处的项目。

Something like that:

像这样的东西:

var array = ['mastercheck', '60154', '60155'];
var index = array.indexOf('mastercheck'); // get the index
array.splice(index, 1); // remove the item

#2


2  

var arr = ['mastercheck',60154,60155];

for(var i=0;i<arr.length;i++){
    if(arr[i] === 'mastercheck'){
        arr.splice(i,1);
    } 
}

console.log(arr);

#3


1  

Use this code jsFiddle

使用此代码jsFiddle

var arr = ['mastercheck', '60154', '60155'];
var index = arr.indexOf('mastercheck');
arr.splice(index, 1);