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

Filter the objects that have the same field value of the value that we provide

My goal is to show only banners that have the same CODE provided in the KEYS array to filter. So I only want the banners which have the same code contained in the array KEYS. Thanks for the help.

const keys = ['343747', '213747', '123444']
export const banners = [
    {
    id: 1,
    name: 'Cashback',
    code: '343747',
    },
    {
    id: 2,
    name: 'Cosmetic',
    code: '213747',
    },
    {
    id: 3,
    name: 'Jeans',
    code: '123444',
    },
    {
    id: 4,
    name: 'WelcomeBonus',
    code: '54344',
    },
]

Number 4 has a code that is not present in the KEYS array so it shouldn’t be shown.

I tried to use the FILTER, REDUCE or MAP method but I couldn’t get to the solution. Thanks to anyone who can help me.

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 :

You can use const filteredBanners = banners.filter((banner) => keys.includes(banner.code))

filter will return an array of values from the original array that satisfies the given function.

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