12

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.

A J
  • 3,827
  • 13
  • 38
  • 52
vissu
  • 1,821
  • 7
  • 36
  • 51

8 Answers8

16

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:

a = a.filter(function(val) {
    return val !== 0;
});
Tim Down
  • 306,503
  • 71
  • 443
  • 520
  • You could just `return val;` as well – Brian Leishman Sep 16 '17 at 16:42
  • @BrianLeishman: If all your array members are guaranteed to be numbers then yes, but it's not quite the same in general: using `return val` will filter out any falsy value, such as `false` or an empty string, rather than just `0`. – Tim Down Sep 18 '17 at 09:16
  • Yeah it was specifically in this example, I figured since you had the `!==` that it made sense here – Brian Leishman Sep 18 '17 at 13:27
15

Use splice method in javascript. Try this function:

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
Harry Joy
  • 57,133
  • 30
  • 158
  • 207
  • what is 1. I think i need to use "0" insted of "1" in my case right? – vissu Apr 28 '11 at 11:25
  • @vissupepala: No. Read the documentation: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/splice . It means remove 1 element starting from index `i`. – Felix Kling Apr 28 '11 at 11:26
  • Nope. Its the number of element you want to remove. you have to pass `0` in `arrayElement` – Harry Joy Apr 28 '11 at 11:26
  • 11
    That will fail if the array has two consecutive zeroes. – Tim Down Apr 28 '11 at 13:41
  • 3
    @TimDown is right, you would need to do `i--` in the `if` statement to solve that. – blex Mar 26 '15 at 21:05
  • [Looping through array and removing items, without breaking for loop](https://stackoverflow.com/questions/9882284) – adiga Apr 12 '21 at 06:15
14

Here's one way to do it:

['0','567','956','0','34'].filter(Number)
jesal
  • 7,584
  • 5
  • 49
  • 55
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!

Adria
  • 8,146
  • 4
  • 36
  • 28
3

For ES6 best practice standards:

let a = ['0','567','956','0','34'];


a = a.filter(val => val !== "0");

(note that your "id's" are strings inside array, so to check regardless of type you should write "!=")

Eric Aya
  • 69,000
  • 34
  • 174
  • 243
Adrian Swifter
  • 79
  • 1
  • 1
  • 9
2
ids.filter(function(x) {return Number(x);});
avoliva
  • 2,820
  • 4
  • 20
  • 36
2

Below code can solve your problem

 for(var i=0; i<ids.length;i++ )
 { 
    if(ids[i]=='0')
        ids.splice(i,1); 
  } 
Ammu
  • 4,837
  • 9
  • 32
  • 34
1

I believe, the shortest method is

var newList = ['0', '567', '956', '0', '34'].filter(cV => cV != "0")

You could always do,

listWithZeros = ['0', '567', '956', '0', '34']
newList = listWithZeros.filter(cv => cv != "0")

The newList contains your required list.

Explanation

Array.prototype.filter()

This method returns a new array created by filtering out items after testing a conditional function

It takes in one function with possibly 3 parameters.

Syntax:

Array.prototype.filter((currentValue, index, array) => { ... })

The parameters explain themselves.

Read more here.

Nithin Sai
  • 611
  • 7
  • 16