0

Suppose I want to write a function that removes all zeros in an Array.

var removeZero = function(nums) {
        const nonZ = nums.filter(myFunc);
        function myFunc(value) {
                return value !== 0;
        }
    return nonZ;
}

The above code would work. Sure. Now I can just do the following and get an array without 0

var someNum = [2,0,9,0,0,34];
var someNum = removeZero(someNum);

Now my real question is, may I directly change the removeZero function's argument, and not have to create a nonZ and return it?

I wrote the following code but didn't work:

var removeZero = function(nums) {
        nums.filter(myFunc);
        function myFunc(value) {
                return value !== 0;
        }
}

var someNum = [2,0,9,0,0,34];
removeZero(someNum);  // this fails changing the someNum array

How can I solve this, thank you

Yani
  • 35
  • 4

4 Answers4

0

You need to return the result that is calculated in your function:

var removeZero = function(nums) {
    return nums.filter(myFunc);
    function myFunc(value) {
            return value !== 0;
    }
}

var someNum = [2,0,9,0,0,34];
console.log(removeZero(someNum));
Carsten Massmann
  • 26,510
  • 2
  • 22
  • 43
0

The filter() method does not replace the original array. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

You could either return the new array like you've done in your first example, or implement your own filtering.

for (let i = nums.length - 1; i >= 0; i--) {
  if (nums[i] == 0) {
    nums.splice(i, 1); // Remove the item from the original array if it's zero
  }
}
Alon L
  • 152
  • 1
  • 5
  • This won't work. See https://stackoverflow.com/questions/9882284/looping-through-array-and-removing-items-without-breaking-for-loop – Barmar Sep 07 '21 at 21:50
0

filter() doesn't modify the array by itself. You could use splice() to replace the array contents with the result of filter().

var removeZero = function(nums) {
  nums.splice(0, nums.length, ...nums.filter(myFunc));

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

var someNum = [2,0,9,0,0,34];
removeZero(someNum);
console.log(someNum);
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

filter will always return a new array based on how the elements are returned by the callback. So you need to return that new array from the function.

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

function removeZero(nums) {
  return nums.filter(notZero);
}

const someNum = [2, 0, 9, 0, 0, 34];
console.log(removeZero(someNum));

If you want to mutate the existing array you could use a loop and splice out the elements that are zero, but the first example is much better.

const someNum = [2, 0, 9, 0, 0, 34];

function isZero(value) {
  return value === 0;
}

function removeZero() {
  for (let i = someNum.length - 1; i >= 0; i--) {
    if (isZero(someNum[i])) someNum.splice(i, 1);
  }
}

removeZero();
console.log(someNum);
Andy
  • 61,948
  • 13
  • 68
  • 95