-2

i have an array as below.

var array_numbers = [0,1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,9]; 

How can all the occurrences of zero be removed from the array and get the non zero numbers in array?

var result_numbers = [1,2,3,4,5,6,7,8,9];
david
  • 11
  • 3
  • 3
    What have you tried? Did you look at Array's [filter()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) function? – Patrick Evans Sep 05 '17 at 22:50
  • 1
    Possible duplicate of [delete zero values from array with javascript](https://stackoverflow.com/questions/5817756/delete-zero-values-from-array-with-javascript) – Dalorzo Sep 05 '17 at 23:00
  • https://stackoverflow.com/q/25361799/1959948 – Dalorzo Sep 05 '17 at 23:01

4 Answers4

2

You can use a Javascript filter function.

First, define a function that will return true if you wish to keep the value or false if you wish to remove it. The value to check will be passed as a parameter to the function.

function removeZeros(value) {
  return value !== 0;
}

You can then use that function in the filter method on your array.

var result_numbers= array_numbers.filter(removeZeros);
Wayne Allen
  • 1,605
  • 1
  • 10
  • 16
1

Probably easiest to just filter each item through Boolean:

const arr = [0,1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,9]

console.log(arr.filter(Boolean))
Rob M.
  • 35,491
  • 6
  • 51
  • 50
  • nother wrong answer! your `arr`is still `[0,1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,9]`. aside from the fact that it would also filter all other falsy values. – Bekim Bacaj Sep 05 '17 at 23:14
  • @BekimBacaj it's only a "wrong answer" if you are defining a correct answer as one that necessarily mutates the original array. OP did not ask "how do I destructively remove elements from an array?", I (and others) determined that they were asking how to get from A => B (and Array.filter is a perfectly fine way of doing that). If OP assigns the result of `arr.filter` to a variable they will have their desired array. – Rob M. Sep 05 '17 at 23:22
  • Yes he did "remove particular number from an Array" Doesn't mean create a new Array. And "a particular number" certainly does not mean 'remove zeros' explicitely, and of course not, any falsy values. – Bekim Bacaj Sep 05 '17 at 23:28
0

array_numbers = [0,1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,9]; 

for(var x in array_numbers)array_numbers[x] === 0 ? array_numbers.splice(x,1) : 0;

/* See the result */
console.log( JSON.stringify(array_numbers) );
Bekim Bacaj
  • 5,707
  • 2
  • 24
  • 26
0

As a complement to Wayne's answer you can also use an ES6 Arrow Function to simplify even further the code.

That would look like this:

var array_numbers = [0,1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,9]; 

console.log(array_numbers.filter(x => x !== 0));

And as Rob M stated 0 evaluates to false, so you can trim it down a bit more with just:

array_numbers.filter(x => x);

Example:

var array_numbers = [0,1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,9]; 

console.log(array_numbers.filter(x => x));

Note that in this last case, values that evaluate to false such as false, '', etc would also be removed.

Isac
  • 1,834
  • 3
  • 17
  • 24
  • Or just `array_numbers.filter(x => x)` since `0` is falsey – Rob M. Sep 05 '17 at 23:14
  • an additional wrong answer, you are not removing anything from the given array. – Bekim Bacaj Sep 05 '17 at 23:17
  • @BekimBacaj his example implies two different arrays, as the variables storing them have different names. So it seems unclear if he really needs the original array to be changed or if a copy will suffice. – Isac Sep 05 '17 at 23:19
  • @Isac wrong, it does not - it says "remove particular number from an Array" Furthermore it could be any particular number.. – Bekim Bacaj Sep 05 '17 at 23:24
  • 1
    @BekimBacaj Still unclear to me. Note the wording on it - "get the non zero numbers in array". But i'll let post owner speak for himself as to what he really needs. Furthermore in most cases a filtered copy will do just fine, it really depends on the situation. – Isac Sep 05 '17 at 23:27
  • Citing the original question here: "How to remove particular Number from an array via JS?" as I'm not able to read it loud for you. Creating a copy instead of cleaning the original array - will cause trouble for the team. And using (still experimental) nonstandard syntax, does not yield usable Production code. – Bekim Bacaj Sep 05 '17 at 23:37
  • @BekimBacaj `array_numbers = array_numbers.filter(x => x !== 0)` there, original with the removed values. As to the Production code, if you read the answer you see that it is stated as a complement with ES6 features, therefore it may not be supported in some browsers/environments. – Isac Sep 05 '17 at 23:40