0
const isIncluded = ["1","2"]
        .includes("1") || ["1","2"]
        .includes("2") ;

how can I reformat this monstrosity in a more elegant way

2 Answers 2

1

For checking an array with another array, you could iterate one array and check the value with includes.

array.some(v => isIncluded.includes(v))
Sign up to request clarification or add additional context in comments.

1 Comment

That looks so much better thank you Nina!
1

You need something like this?

const checkIncluded = (arr1, arr2) => arr1.some(item => arr2.includes(item));

If any item from arr1 is inside arr2 returns true.

Example:

const isIncluded1 = checkIncluded(["1", "2"], ["1","2"]) // true
const isIncluded2 = checkIncluded(["1", "2"], ["1"]) // true
const isIncluded3 = checkIncluded(["1", "2"], ["2"]) // true
const isIncluded4 = checkIncluded(["1", "2"], ["3", "5"]) // false

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.