Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Simplifying an Array containing an array of objects

I have this array of customer licenses I am sorting through and creating a live search function with an array that updates properly. The issue I am facing is that I want my filtered array to be an array like this [[{},{},{},{},{}]] currently after typing in a word to filter the array looks like this instead [ [{}], [{},{}], [{},{}] ] or something similar whereas its an array with multiple arrays instead of just one.

I’m sure its something simple that I have overlooked in the past couple of hours trying to figure out how to achieve what I want.

function FilterResults(term, results)
{
    return results.reduce((filtered, group) => 
    {
        const match = group.filter(({ customerName }) => customerName.toLowerCase().includes(term.toLowerCase()));
            
        match.length && filtered.push(match);
        
        return filtered;
    }, []);
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I am not sure if you really want an array inside of another array but
You can do it like this one.
I have spread and then pushed the match array and returned the result in a new array.

  function FilterResults(term, results)
{
    const res = results.reduce((filtered, group) => 
    {
        const match = group.filter(({ customerName }) => customerName.toLowerCase().includes(term.toLowerCase()));
            
    match.length && filtered.push(...match);
    
    return filtered;
}, []);

return [res]
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading