var indices = [];
var array = ["a", "b", "a", "c", "a", "d"];
var element = "a";
var idx = array.indexOf(element);
while (idx != -1) {
indices.push(idx);
idx = array.indexOf(element, idx + 1);
}
console.log(indices);
// [0, 2, 4]
Now I would like to understand how it works, I never understood it, but still, who knows?
Standard explanations don’t suit me either)))) I don’t understand them, because where do they get the values -1 and for what?
and how can idx be -1 if the report starts from 0 then you need to write that while (idx== array) that is, the element is found, a match is found, add indices to it
I don’t understand this at all, I can only agree that it is necessary, but why and why here you can understand it as you want)It’s clear that there is no -1 index, since the report starts with 0, why write it, and even so it’s not clear
when I don’t understand, the first thing I do is start reading the documentation, but I don’t understand 90% of what they write there, so after that I turn to the forum,
What would the index of grade I be? It doesn’t exist in the array, so you need to return a value that indicates it doesn’t exist. Since any positive number could be a valid array index, you can’t return any of those. Returning false/null/undefined means you’re returning a different type. You could do that, but those can be easily confused with index zero which leads to bugs, so that’s not a great choice. What’s left? Array’s cannot have negative indexes, so returning -1 works well to handle this case.
In general, these kinds of things are considered a Sentinel value
Because the while loop would break immediately every time, it would do nothing.
It is saying continue the loop while it is not returning an invalid result.
So continue when the result works, but stop when the result is invalid.
What the LOOP is trying to do is to find ALL instances of the value in the array.
indexOf will return the first index it finds, or -1 if it finds none.
When is the loop done looping? when it’s found all of them.
How does it know when it’s found all of them? When there’s none left.
So While the indexOf is not -1, it has more work to do.
The second parameter to indexOf tells it which index to start with when searching for the element. idx is the position of the element that was just found, so if you use that for the second parameter it is just going to return idx again since array[idx] == element.
By adding 1, the search resumes with the next index after the one that was just found.
So when you consulted the documentation about array.indexOf() what didn’t you understand about
Array.prototype.indexOf()
The indexOf() method of Array instances returns the first index at which a given element can be found in the array, or -1 if it is not present.
It seems perfectly obvious to me.
In some computer languages the maximum value would be returned
Also the maximum value for a cardinal(unsigned) number is the same binary representation as -1 in a twos-compliment integer value, and that is probably why -1 was chosen for not present, or maybe not.