Home > OS >  How to make filter function that doesn't stop at first false?
How to make filter function that doesn't stop at first false?

Time:06-13

Im trying to filter an array of objects by comparing it to another array by looping through it's values. The problem is that the filter function stops at the first false by default, so if the loop returns (false, true, false) it will not pass the filter.

is there a way around this?

code (I've put simplfied arrays and objects in the code for the example):

const jobs = [
    {id: 1,
    location: 'AA'},
    {id: 2,
    location: 'BB'},
    {id: 3,
    location: 'CC'},
]
const filteredLocations = ['AA', 'CC']
const filteredJobs = jobs.filter(function(el, i) {
    do {
        return el.location === filteredLocations[i];
    }
    while (filteredLocations.length >= i)
})
// this only returns the first object instead of the desired first and third

Thank you!

CodePudding user response:

Use the .filter function as it is intended, any return will be added to the returned array.

const jobs = [
    {id: 1,
    location: 'AA'},
    {id: 2,
    location: 'BB'},
    {id: 3,
    location: 'CC'},
]
const filteredLocations = ['AA', 'CC']
const filteredJobs = jobs.filter(el => {
    return filteredLocations.includes(el.location);
});

sidenote, your code has 2 issues:

  1. it may return false inside the while loop, if it doesnt match the 1st occurrence, hence why the single output you had
  2. you compare filteredLocations.length with the i (index) of the jobs array loop, jobs and filteredLocations are 2 different arrays, so that doesnt make sense to do so

CodePudding user response:

Filter doesn't stop at first false. It works correctly but it cannot return the first and third objects because the third object is on index 2 and you don't have any items in filteredLocations on that index.

CodePudding user response:

I would advise you to replace the do-while loop with a simple finder function as follows:

const jobs = [
    {id: 1,
    location: 'AA'},
    {id: 2,
    location: 'BB'},
    {id: 3,
    location: 'CC'},
]
const filteredLocations = ['AA', 'CC']
const filteredJobs = jobs.filter(function(el, i) {
    return filteredLocations.find((filteredLocation) => filteredLocation === el.location);
})
console.log(filteredJobs);

CodePudding user response:

let filtered = jobs.map(j => filteredLocations.every(fl => fl == j.location));

CodePudding user response:

How to make filter function that doesn't stop at first false ?

filter() never stop at first false. It will check for all the true values which passed the applied condition and return those elements from the array in a new array.

To achieve the requirement, You can simply use the single line of code in the filter function instead of do-while loop.

Demo :

const jobs = [
    {id: 1,
    location: 'AA'},
    {id: 2,
    location: 'BB'},
    {id: 3,
    location: 'CC'},
];

const filteredLocations = ['AA', 'CC'];

const filteredJobs = jobs.filter(({ location }) => filteredLocations.includes(location));

console.log(filteredJobs);

  • Related