user2762414
user2762414

Reputation: 47

.filter in double arrays

Helo. I am beginner in this JavaScript journey, and i just hit a wall. I am learning .filter() function on Array. My exercise is:

Return only the rows in the matrix that have all positive integers

I have no problems with single arrays. For example my code with single array:

function positiveRowsOnly (array) {
    var result = array.filter(function (ind) {
        return ind < 0;
    });
    return result;
};

console.log(positiveRowsOnly([1, 10, -100, 2, -20, 200, 3, 30, 300]));

its quite simple for me to understand that "ind" in .filter will accept every index from given array and check if ind < 0.

What i am struggling is if i have double arrays. Original exercise is with double arrays.

function positiveRowsOnly (array) {
    var result = array.filter(function (ind) {
        return ind < 0;
    });
    return result;
};

console.log(positiveRowsOnly([[1, 10, -100 ], [ 2, -20, 200 ], [ 3, 30,  300 ]])); 

On internet i just can not find any deeper meaning how .filter() works: does filter go in one array and gets each index? Does "ind" gets just the first array and not first array index? I was looking at Math functions or indexOf but no luck. I hope you understand my struggle. Can anyone explain how this can be done or most important, how does .filter work in double arrays? In pseudo code i know, look in array index, if it has a negative number than ignore, else return that array.

Upvotes: 0

Views: 569

Answers (2)

Ele
Ele

Reputation: 33726

The function filter will work the same way regardless of the type of every element within an array. Basically, will loop and test a condition to filter that array.

So, you can use the function every to loop (nested arrays) and check the values from them.

  +----- This is the nested array
  |
  |   +----- This is the function 'every' that will loop
  |   |      and check every element.
  v   v  
  ind.every(function(n) { -+
   return n >= 0           |----> This is the predicate.
  });       ^             -+ 
            |
            |
            +----- This is the test to check every 
                   element within the nested array.

The function every returns true when every element within an array meets a specific predicate (handler, callback, Etc,.), in your case n >= 0.

function positiveRowsOnly(array) {
  var result = array.filter(function(ind) {
    return ind.every(function(n) {
      return n >= 0
    });
  });
  return result;
};

console.log(positiveRowsOnly([
  [1, 10, -100],
  [2, -20, 200],
  [3, 30, 300]
]));
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 1

Chris Jaynes
Chris Jaynes

Reputation: 2907

On internet i just can not find any deeper meaning how .filter() works: does filter go in one array and gets each index? Does "ind" gets just the first array and not first array index?

The Array.prototype.filter function will loop over the elements of an array, and pass each element to the callback function you provide (sometimes referred to as a 'predicate'). The index is actually the second argument to your predicate function.

In your case, each element of your outermost array is also an array. So your filter predicate will be passed each of the inner arrays, one at a time. If you want to filter the inner arrays, you'll need to "loop inside your loop", and call the filter function again to find the positive numbers.

Since you only want to return entire positive rows, you may want to look at other functions like Array.prototype.every, which will return a boolean if the predicate function matches. Then you could return the whole, unfiltered row.

A good trick is to try putting a console.log(ind) inside your filter functions so you can see what each element of that array looks like.

Upvotes: 1

Related Questions