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

How to check if array has values from another array in Javascript in one line?

I have an array called items which contains a list of strings which I want to search against me key_res array which contains some keywords I need either a true or false value on.

This is the code I have so far which works:

const items = ['Quality 1080p', 'Quality 720p', 'Quality 4K'];
const key_res = ['4K', '6K'];
contains_string = items.every(item => {
            if(key_res.some(string => item.includes(string))) {
                return false;
            }
            return true;
        });

Is there a way I can return a boolean value easier than going through a loop and then setting a boolean?

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 :

Use some instead of forEach:

let items = ['Quality 1080p', 'Quality 720p', 'Quality 4K'];
const key_res = ['4K', '6K'];
let bool = items.some(item => key_res.some(string => item.includes(string)));
console.log(bool);

items = ['Quality 1080p', 'Quality 720p'];
bool = items.some(item => key_res.some(string => item.includes(string)));
console.log(bool);
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