Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
183
rated 0 times [  185] [ 2]  / answers: 1 / hits: 5994  / 10 Years ago, wed, july 2, 2014, 12:00:00

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!


More From » arrays

 Answers
2
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);
}

[#44172] Tuesday, July 1, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
harleyaleenag

Total Points: 678
Total Questions: 121
Total Answers: 105

Location: Papua New Guinea
Member since Thu, Jul 9, 2020
4 Years ago
;