0

I'd like to iterate trough a javascript array (nested objects: based on collection+Json) and collect data from its objects. But if the data of an object isn't available, skip it and go to the next object, resp. continue the iteration. Currently, this error appears if the data isn't available in the according object: Uncaught TypeError: Cannot read property 'hk5' of undefined

How can I check first, if the data (hereafter hk5) is available?

Iterating through all "data-objects"

for (var i = 0; i < data.collection.items.length; i++) {
    var data = data.collection.items[i].data[2].value.packet_data_field.application_data_params.hk5;
    console.log(belaMode);
}

Thanks!

1
  • By using an if statement. if statements let you conditional execute code. They look like if (some condition) { /*do something if true */ } else { /* do something if false */} Commented Jul 2, 2014 at 9:26

2 Answers 2

1
for (var i = 0; i < data.collection.items.length; i++) {
    if(typeof(data.collection.items[i].data[2].value.packet_data_field.application_data_params) != 'undefined'){
    var data = data.collection.items[i].data[2].value.packet_data_field.application_data_params.hk5;
    }
    console.log(belaMode);
}
Sign up to request clarification or add additional context in comments.

1 Comment

You've gone one property too far. The OP wants to test if application_data_params is undefined.
0
if(data.collection.items[i].data[2].value.packet_data_field.application_data_params!=undefined){}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.