I am fairly new to JS and have a project to find the index of an array element, without using indexOf built in function. I have tried to search for solutions, but all I get are examples of the aforementioned built in function, which I already know how to use. Can anyone provide a simple example for me to go on? My mind goes towards something like this, but please note I am new to this and it is an academic exercise that I really want to understand:
var index;
var target = 10;
for(var val in collection){
if(collection[val] === target){
index = val;
}
return index;
}
6 Answers 6
This attempt is almost correct. You seem to already understand of what is required: loop over the elements of the array until you find a match, and then return the index of that match.
You've made a few mistakes, though. Let's walk through some improvements, step by step.
Your
return
statement is inside the loop, but outside of theif
. You only want toreturn
if you're found a match. Currently, you always return after the first iteration of the loop!function myIndexOf(collection, target) { var index; for(var val in collection){ if(collection[val] === target){ index = val; return index; } } }
There is no need for a separate
index
variable. You can returnval
as soon as you determine it's the correct answer.function myIndexOf(collection, target) { for(var val in collection){ if(collection[val] === target){ return val; } } }
You should loop using a numeric
for
loop, not afor-in
loop.for-in
loops are not guaranteed to have a set order, so you may not always get the lowest index that is a match. (Also,for-in
could match on non-numeric property names, which might not be what you want.)function myIndexOf(collection, target) { for(var val=0; val<collection.length; val++){ if(collection[val] === target){ return val; } } }
To act just like
indexOf
, you could return-1
in the case that you don't find a match.function myIndexOf(collection, target) { for(var val=0; val<collection.length; val++){ if(collection[val] === target){ return val; } } return -1; }
1 Comment
Note: for..in should not be used to iterate over an Array where the index order is important.
var find_index = function(collection, item) {
for (var i = 0; i < collection.length; ++i) {
if (collection[i] === item) {
return i;
}
}
};
find_index([5,4,3,2,1], 5)
1 Comment
When looping through an array you should use a simple for
loop from index 0
to Length-1
of your array. Don't use for...in
because it can iterate properties of the array that aren't the actual contents of the cells and the order is not guaranteed. You want to find the first match to have the same behavior as .indexOf
. So the correct implementation would look something like this:
function myIndexOf(array, target) {
for (var i=0; i < array.length; i++) {
if (array[i] === target) {
return i;
}
}
// item was not found
return -1;
}
Comments
You can use in statement to iterate an array, assumings keys are values. Here val will be 0, 1, 2... so you can return it.
Then, you can use the return inside the if : it will stop the function and return the value just right you find what you are loonking for.
The === is the strict comparaison operator, checking var type, allowing you to test this is the exact value you are looking for.
You can add a return with an other value (here -1) if the value is not found at the end of the loop.
function myIndexOf(array, search) {
for(var val in array){
if(array[val] === search){
return val;
}
}
return -1;
}
var myArray = ['a', 'b', 'c'];
console.log(myIndexOf(myArray, 'c')); //2
console.log(myIndexOf(myArray, 'f')); //-1 <- Not found
Comments
This would work:
var index = 0;
var target = 'c';
var collection = ['a', 'b', 'c', 'd'];
function findIndex(){
for(var val in collection) {
if(collection[val] === target){
return index;
}
index++;
}
}
findIndex();
Comments
You can use every as well. Iterate through the array, return true if value doesn't match value being searched so every continues, otherwise set ind and return false.
var getIndex = function(arr, match){
var ind = -1;
arr.every(function(val, index) {
if (val === match){
ind = index;
return false;
}
return true;
});
return ind;
}
getIndex([1, 2, 3], 2);
if
statement.collection
is actually an array, don't usefor...in
, use a regular loopfor (var i =0; i < collection.length; i++)
. Then in the body of your loop check ifcollection[i] == target
and if so returni
.