admin管理员组文章数量:1388805
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]
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!
-
In the first example, you're calling
indexOf
on the first element of the array. This means that you're actually doing"true".indexOf("true")
, which is a (weird) way of paring two strings. You don't need any of that. You can just usea[i] == "true"
– Michele Lambertucci Commented Aug 24, 2020 at 9:58 - you can same functionality by type-checking , instead of using indexOf(); arr[i] === true like that. – Papai from BEKOAIL Commented Aug 24, 2020 at 9:58
4 Answers
Reset to default 3IndexOf returns the first index at which given element can be found in the array, or -1 if it is not present, meaning, you'll only find the first index (0).
let arr = [true, true, false, false, false, false, false, false];
let newArr = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i]) {
newArr.push(i);
}
}
console.log(newArr);
When you refer to arr[i]
in your first code block, your referring to the string value at index i
. This means that when you do:
if (arr[i].indexOf("true") >= 0) {
... you're performing .indexOf()
on arr[i]
which is either "true"
or "false"
(not your array). When you perform indexOf() on a string it will give you the first index of where the passed in string appears. In the case of "true"
, if arr[i]
is "true" then you'll get index 0
as the string true starts at index 0. In the case of arr[i]
being "false" then .indexOf() will return -1 since "true" doesn't appear in your string. As you can see, your .index()
is just checking if "true"
appears in one of your string array values. However, since your array values only consist of either "true" or "false" there is no need to use .indexOf()
in your first example, but rather you can just check whether the current item in the array is equal to "true"
and if it is, then push the current index to your array.
In the case of your second example, your values are booleans. This means arr[i]
now is either the boolean value true
or false
. Booleans do not have an .indexOf()
method, hence why you're getting an error. However, as pointed out above, you don't need to use .indexOf()
checking if arr[i] is true
, and instead, just need to check if arr[i]
is true
and if it is, then .push()
the current index like so:
let arr = [true, true, false, false, false, false, false, true];
let newArr = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i]) { // roughly the same as if(arr[i] == true)
newArr.push(i);
}
}
console.log(newArr);
Hmm you could reduce it. There is an third argument the current index i
let arr = [true, true, false, false, false, false, false, true];
let result = arr.reduce((a,v,i) => {
if(v === true) a.push(i);
return a;
},[])
console.log(result);
Or with forEach
it also has an index argument
let arr = [true, true, false, false, false, false, false, false];
let newArr = [];
arr.forEach((el, i) => {
if(el === true) newArr.push(i);
})
console.log(newArr);
Basically indexOf
is the method of Array
and String
type. So when you do is with Boolean value, it would not work
Array.prototype.indexOf()
String.prototype.indexOf()
So in your case, just case the value to String
let arr = [true, true, false, false, false, false, false, false]
let newArr = []
for (let i = 0; i < arr.length; i++) {
if (String(arr[i]).indexOf(true) >= 0) {
newArr.push(i)
}
}
console.log(newArr)
No need to worry of casting the value passed to indexOf
param to String, because this method would cast that for you
String.prototype.indexOf() > Syntax > Parameters
ECMAScript Language Specification
本文标签: How do I use indexOf method with multiple Boolean values in an array (Javascript)Stack Overflow
版权声明:本文标题:How do I use indexOf method with multiple Boolean values in an array (Javascript)? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744626971a2616316.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论