I have this array:
var array = [400, 4000, 400, 400, 4000];
How can I get the index of the first element with value greater than 400?
You can use a simple for
loop and check each element.
var array = [400, 4000, 400, 400, 4000];
var result;
for(var i=0, l=array.length; i 400){
result = i;
break;
}
}
if(typeof result !== 'undefined'){
console.log('number greater than 400 found at array index: ' + result);
} else {
console.log('no number greater than 400 found in the given arrry.');
}
Read up: for - JavaScript | MDN