How can I return an array of Booleans as a result of Array.prototype.every() method?

Viewed 53

I am trying to map an array in order to get a Boolean for each iteration after comparing two arrays.

  1. Compare if the values of array a are included into b.

  2. Get an array of Booleans, returning the result of each iteration made in the every() method

I thought of using a combination of every() and map()

This is how far I went:

let a = [1,2,4];
let b = [1,2,3]

let answer = a.every(num => b.includes(num))


console.log(answer)
// returns false

I tried placing the callback of every() inside a map() method without success.

I know that every() iterates, so somehow it should be easy to the an array of each iteration like:

[true,true,false]

Thanks in advance!

1 Answers

    let a = [1,2,4];
    let b = [1,2,3]
    
    let answer = a.map(num => b.includes(num))
    
    
    console.log(answer)

Use map function

Map Function docs

Related