As I understand when you said on one of your comment output 1,2,3,4,5 that you need the missing ids -- In your case there are 2,4
var team = [{id:"1", name:"chelsea"}, {id:"3", name:"mu"}, {id:"5", name:"arsenal"}];
var empty_ids = 0;
for(var i = 0; i < 5; i++) {
if(team[i] && typeof team[i] !== 'undefined' && team[i] !== null) {
if(parseInt(team[i].id) !== i + 1){ // check if id on the array not equal the i + 1 from the loop
for( var k= 1 ; k < parseInt(team[i].id) - empty_ids ; k++){
console.log(empty_ids + k +" missing");
}
console.log(team[i].id);
}else{
console.log(team[i].id);
}
empty_ids = parseInt(team[i].id);
}else{
if(empty_ids <= i){
console.log(empty_ids + 1 + " undefined team[i]");
empty_ids = empty_ids + 1;
}
}
}
Note: this code will work even if you change the team array
var team = [{id:"1", name:"chelsea"}, {id:"5", name:"arsenal"}];
//or
var team = [{id:"1", name:"chelsea"}, {id:"3", name:"mu"}, {id:"4", name:"arsenal"}];
//or
var team = [{id:"1", name:"chelsea"}, {id:"4", name:"arsenal"}];
So please try to change var team = with suggested values .. I added a missing and undefined to let you notice from where the console.log comes