Hai All, I have an array with name "ids" and some values like ['0','567','956','0','34']. Now i need to remove "0" values from this array. ids.remove("0"); is not working.
Please let me know the solution.
Hai All,我有一个名为“ids”的数组,有些值如['0','567','956','0','34']。现在我需要从这个数组中删除“0”值。 ids.remove( “0”);不管用。请让我知道解决方案。
6 个解决方案
#1
14
Use splice method in javascript. Try this function:
在javascript中使用splice方法。试试这个功能:
function removeElement(arrayName,arrayElement)
{
for(var i=0; i<arrayName.length;i++ )
{
if(arrayName[i]==arrayElement)
arrayName.splice(i,1);
}
}
Parameters are:
参数是:
arrayName:- Name of the array.
arrayElement:- Element you want to remove from array
#2
11
Here's one way to do it:
这是一种方法:
['0','567','956','0','34'].filter(Number)
#3
6
Here's a function that will remove elements of an array with a particular value that won't fail when two consecutive elements have the same value:
这是一个函数,它将删除具有特定值的数组元素,当两个连续元素具有相同值时,该值不会失败:
function removeElementsWithValue(arr, val) {
var i = arr.length;
while (i--) {
if (arr[i] === val) {
arr.splice(i, 1);
}
}
return arr;
}
var a = [1, 0, 0, 1];
removeElementsWithValue(a, 0);
console.log(a); // [1, 1]
In most browsers (except IE <= 8), you can use the filter()
method of Array objects, although be aware that this does return you a new array:
在大多数浏览器中(IE <= 8除外),您可以使用Array对象的filter()方法,但要注意这确实会返回一个新数组:
a = a.filter(function(val) {
return val !== 0;
});
#4
4
For non-trivial size arrays, it's still vastly quicker to build a new array than splice or filter.
对于非平凡大小的数组,构建新数组的速度比拼接或过滤器快得多。
var new_arr = [],
tmp;
for(var i=0, l=old_arr.length; i<l; i++)
{
tmp = old_arr[i];
if( tmp !== '0' )
{
new_arr.push( tmp );
}
}
If you do splice, iterate backwards!
如果你拼接,则向后迭代!
#5
2
Below code can solve your problem
下面的代码可以解决您的问题
for(var i=0; i<ids.length;i++ )
{
if(ids[i]=='0')
ids.splice(i,1);
}
#6
1
ids.filter(function(x) {return Number(x);});
#1
14
Use splice method in javascript. Try this function:
在javascript中使用splice方法。试试这个功能:
function removeElement(arrayName,arrayElement)
{
for(var i=0; i<arrayName.length;i++ )
{
if(arrayName[i]==arrayElement)
arrayName.splice(i,1);
}
}
Parameters are:
参数是:
arrayName:- Name of the array.
arrayElement:- Element you want to remove from array
#2
11
Here's one way to do it:
这是一种方法:
['0','567','956','0','34'].filter(Number)
#3
6
Here's a function that will remove elements of an array with a particular value that won't fail when two consecutive elements have the same value:
这是一个函数,它将删除具有特定值的数组元素,当两个连续元素具有相同值时,该值不会失败:
function removeElementsWithValue(arr, val) {
var i = arr.length;
while (i--) {
if (arr[i] === val) {
arr.splice(i, 1);
}
}
return arr;
}
var a = [1, 0, 0, 1];
removeElementsWithValue(a, 0);
console.log(a); // [1, 1]
In most browsers (except IE <= 8), you can use the filter()
method of Array objects, although be aware that this does return you a new array:
在大多数浏览器中(IE <= 8除外),您可以使用Array对象的filter()方法,但要注意这确实会返回一个新数组:
a = a.filter(function(val) {
return val !== 0;
});
#4
4
For non-trivial size arrays, it's still vastly quicker to build a new array than splice or filter.
对于非平凡大小的数组,构建新数组的速度比拼接或过滤器快得多。
var new_arr = [],
tmp;
for(var i=0, l=old_arr.length; i<l; i++)
{
tmp = old_arr[i];
if( tmp !== '0' )
{
new_arr.push( tmp );
}
}
If you do splice, iterate backwards!
如果你拼接,则向后迭代!
#5
2
Below code can solve your problem
下面的代码可以解决您的问题
for(var i=0; i<ids.length;i++ )
{
if(ids[i]=='0')
ids.splice(i,1);
}
#6
1
ids.filter(function(x) {return Number(x);});