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?
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)
This should be a nice one-liner to solve the problem.
first_array.reduce((ac, e) => ac && second_array.includes(e), true)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With