I am iterating through an object. However, it is not catching all the keys. It is catching only a few. It is especially not catching organic_results key. I don't understand why. Kindly help.
for (var key in this.writeItOutput){
if(key == "inline_people_also_search_for"){
console.log("inline_people_also_search_for")
}
}
if(key == 'organic_results'){
console.log("inside organic_results");
}
if(key =="related_questions"){
console.log("inside related questions");
}
if(key =="related_searches"){
console.log("Inside related_searches");
}
}
All the keys are:
you have a bracket that closes your for-in loop just before this line:
if(key == 'organic_results'){
so the steps your script executes are the following:
start for-in loop
set key variable to a key from this.writeItOutput iteration
check whether the key equals to "inline_people_also_search_for"
go to step 2 until all keys in this.writeItOutput are iterated
finish the loop
run if blocks
So when you reach the step 5, your key variable equals ONLY ONE key in this.writeItOutput. What you may want to do is to put all these if blocks inside your for-in loop:
for (var key in this.writeItOutput){
if(key == "inline_people_also_search_for"){
console.log("inline_people_also_search_for")
}
// } - this bracket is the problem
if(key == 'organic_results'){
console.log("inside organic_results");
}
if(key =="related_questions"){
console.log("inside related questions");
}
if(key =="related_searches"){
console.log("Inside related_searches");
}
}