Why function returns filtered boolean array

orso

I learn js and trying to write filter method without using it. So I need to my function return filtered array based on function, which passed as a parameter. And it does but it's returned boolean array and I don't understand why.

My code:

 function myFilter(arr, func) {
      const result = [];
        for (let el of arr) {
            result.push(func(el));
        }
        return result;
    }

Calling with some numbers:

myFilter([2, 5, 1, 3, 8, 6], function(el) { return el > 3 })

It should've returned [5, 8, 6] but instead I got [false, true, false, true, true]. I don't get why can someone please explain it to me?

Jeffrey Ram

It's returning a boolean array because you're pushing booleans into the result array. func(el) or function(el) { return el > 3 } returns a boolean.

What you want is to push elements/numbers into the result array based on the condition of func(el)

Try

function myFilter(arr, func) {
    const result = [];
    for (let el of arr) {
        if (func(el)) {
            result.push(el);
        }
    }
    return result;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why does TypeScript interpret filtered string array as string | boolean array

Why my filtered value in array using NSPredicate returns a repeated output?

Why is this function returns an empty array?

Function returns boolean not string

Computercraft function that returns an array, use first element for boolean

push to Array in async function is not filtered

Function that returns boolean with nested subscription

Check that a function always returns boolean

To get a truthy condition(array) from a nested array, for now the function returns boolean value true

Why does the filter() function not return my expected type-filtered array?

Cannot change Numpy array elements filtered by boolean indexing

function that accepts a function and a list and returns boolean

Function that takes a function and list and returns Boolean

Why is it possible to write a boolean array to a parcel but not a boolean?

Karate Framework - Why Javascript function returns an array with NaN values?

Why passing function object to Object.keys() returns empty array?

Google Cloud Function - Why bucket always returns empty array of files?

Why does my function only returns the first object from the array

Why Unpack function returns array with starting index 1 in PHP

Why Array sort function returns the changed result in Javascript?

Testing a function which returns an object that returns a function which return a boolean

Why does sorting an associative array by a boolean value using a comparison function reverse the array order?

Why is there no ExpectedConditions method that returns Boolean for element visibility?

Unsure why this for/if code returns the wrong Boolean variable

Why is there an undefined index on a Boolean array?

Elastic Search: Why my filtered query returns nothing?

Assigning Boolean returns Boolean, but assigning integer does not. Why?

Populating column based on function that returns boolean

Boolean JavaScript function that returns if current date is an Holiday