简体   繁体   English

作为 Array.prototype.every() 方法的结果,我如何返回一个布尔数组?

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

I am trying to map an array in order to get a Boolean for each iteration after comparing two arrays.我正在尝试 map 一个数组,以便在比较两个 arrays 之后为每次迭代获得一个 Boolean。

  1. Compare if the values of array a are included into b .比较数组a的值是否包含在b中。

  2. Get an array of Booleans, returning the result of each iteration made in the every() method获取一个布尔数组,返回在every()方法中进行的每次迭代的结果

I thought of using a combination of every() and map()我想过使用every()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.我尝试将every()callback放在map()方法中,但没有成功。

I know that every() iterates, so somehow it should be easy to the an array of each iteration like:我知道every()迭代,所以不知何故,每次迭代的数组应该很容易,例如:

[true,true,false]

Thanks in advance!提前致谢!

 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

Map Function docs Map Function 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM