I'm just new to javascript and I'm having trouble getting the position of multiple boolean values using indexOf()
method in an array. I've seen this similar post "indexOf method with multiple values" but it seems this only works on strings:
let arr = ["true", "true", "false", "false", "false", "false", "false", "true"];
let newArr = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i].indexOf("true") >= 0) {
newArr.push(i);
}
}
console.log(newArr) // result: (3) [0, 1, 7]
But, my codes look like below using boolean values in an array:
let arr = [true, true, false, false, false, false, false, true];
let newArr = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i].indexOf(true) >= 0) {
newArr.push(i);
}
}
console.log(newArr) // result: Uncaught TypeError: arr[i].indexOf is not a function
How could I make the same result (position: [0, 1, 7) like on string values in an array? I did try use join()
method like ('"' + arr.join('","') + '"')
to convert boolean into string. It only looks the same but now in one string value, so, it does not still work. Please help with my Javascript journey. Thanks for your help!