admin管理员组文章数量:1406444
I need to return a boolean value to assign to my form's checkboxes by checking if the value exists in the array.
For this I've been trying to use .map()
by traversing a list of ID's and checking if this id exists within that list by returning an array of Boolean values.
const arrayOne = ['id1', 'id2', 'id3', 'id4', 'id5'];
const arrayTwo = ['id4'];
const booleanValues = [false, false, false, true, false]; // expected oute
How to check if the id exists in another array returning boolean values using .map()
in a simplified and readable way?
I need to return a boolean value to assign to my form's checkboxes by checking if the value exists in the array.
For this I've been trying to use .map()
by traversing a list of ID's and checking if this id exists within that list by returning an array of Boolean values.
const arrayOne = ['id1', 'id2', 'id3', 'id4', 'id5'];
const arrayTwo = ['id4'];
const booleanValues = [false, false, false, true, false]; // expected oute
How to check if the id exists in another array returning boolean values using .map()
in a simplified and readable way?
- I have already tried some forms, but none that was legible of easy understanding ... I know how easy it is but I am not able to create something simple with readability. – Luiz Commented Sep 24, 2018 at 0:24
1 Answer
Reset to default 4I'd just use includes
to see if the value of arr
is present in checkVals
:
var arr = [1, 2, 3];
var checkVals = [2];
var results = arr.map(n => checkVals.includes(n));
console.log(results);
// [false, true, false]
Instead of using an array for checkVals
, you could also use a Set for better performance. That's likely not necessary here though.
本文标签: javascriptReturn true or false using map arraysStack Overflow
版权声明:本文标题:javascript - Return true or false using map arrays - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745015091a2637788.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论