moses toh
moses toh

Reputation: 13162

How can I check index in object array exist or no in javascript?

My javascript code like this :

<script type="text/javascript">
    var team = [{id:"1", name:"chelsea"}, {id:"3", name:"mu"}, {id:"5", name:"arsenal"}];
    for(var i = 0; i < 5; i++) {
        if(team[i].id || typeof team[i].id !== 'undefined' || team[i].id !== null) {
            console.log(team[i].id)
        }
        else {
            console.log(i+1)
        }
    }
</script>

If the code run, on the console exist error like this :

Uncaught TypeError: Cannot read property 'id' of undefined

Whereas I had add condition if the variable not exist

How can I solve it?

Upvotes: 0

Views: 125

Answers (3)

Mohamed-Yousef
Mohamed-Yousef

Reputation: 24001

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

Upvotes: 1

Mμ.
Mμ.

Reputation: 8542

The main problem is that you are checking a property of an object, in this case team[i], that might be undefined.

For example, if you console.log(team[4]), which will point to undefined because there is only 3 objects in team. Checking a property of undefined will result in an error.

var arr = [1,2,3]

// this will be undefined
var element = arr[4]

console.log(element.toString)

So, you should also is check if team[i] is NOT undefined. Your code should look something like the code below. I am assuming that you want to print 1, 2, 3, 4, 5.

var team = [{id:"1", name:"chelsea"}, {id:"3", name:"mu"}, {id:"5", name:"arsenal"}];

for(var i = 0; i < 5; i++) {
    // check if team[i] is not undefined using (team[i] !== undefined)
    if((team[i] !== undefined) && ( team[i].id || typeof team[i].id !== 'undefined' || team[i].id !== null)) {
        var currentId = team[i].id > i + 1 ? i + 1 : team[i].id;
        console.log(currentId)
    }
    else {
        console.log(i + 1);
    }
}

Upvotes: 0

Raj
Raj

Reputation: 505

Few observations,

  1. Instead of hardcoded value '5' inside for loop you should use array length i.e. in this case team.length
  2. Instead of logging 'i+1' you can log 'i' as array index starts from '0'. Or simply you can print object/team name.

here is modified code,

<script type="text/javascript">
    var team = [{id:"1", name:"chelsea"}, {id:"3", name:"mu"}, {id:"5", name:"arsenal"}];
    for(var i = 0; i < team.length; i++) {
        if(team[i].id || typeof team[i].id !== 'undefined' || team[i].id !== null) {
            console.log(team[i].id)
        }
        else {
            console.log('team id not found for index ' + i);
        }
    }
</script>

Upvotes: 0

Related Questions