5

Given an array of strings:

const first_array = ['aaa', 'bbb', 'ccc']

and another array of strings:

const second_array = ['aaa', 'bbb', 'ccc', 'ddd', 'eee']

How can I return true if all the strings from first_array are present in the second_array and false otherwise?

2

2 Answers 2

10

You can use every() method to check whether every element is contained in second_array:

const result = first_array.every(f => second_array.includes(f))

An example:

const first_array = ['aaa', 'bbb', 'ccc']
const second_array = ['aaa', 'bbb', 'ccc', 'ddd', 'eee']

const result = first_array.every(f => second_array.includes(f))
console.log(result)

Sign up to request clarification or add additional context in comments.

2 Comments

Ooooh, forgot about the every method. Very clever
@BrandonDyer thanks! Yeah, it is very good method!:)
1

This should be a nice one-liner to solve the problem.

first_array.reduce((ac, e) => ac && second_array.includes(e), true)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.