Rishav Pandey
Rishav Pandey

Reputation: 692

All the statements are not executing in for ...in loop

I'm trying to run several statements inside for loop of object, but only first line of code is executing inside the for loop, rest lines are not executing.

addFinalQuestionDetail (items) {
var checkEmpty = false; // Check weather all the data is filled to perform further action
console.log(items);
for (var item in items)
  console.log(item);
  console.log(items[item]);
  console.log(typeof(items[item]));
}

Here items are the object name passed in the function, writtem below:-

This is my object:

addQuestionValueDetails = {
    questionTemplateType: " ",
    questionTemplateName: " " ,
    questionTemplateId: " " ,
    questionTitle: " ",
    questionOptionCount: 0,
    questionDuration: "",
    questionOptionList: [ ],
    questionBlankList: [ ],
    correctOptionList: [ ],
    questionDifficulty: 0,
    questionTags: " ",
    isNewQuestion: false,
    isEditedOption: false
  }

Only first console.log is executing properly, then rest two lines are not executing desired times loop is running. If I'm commenting first statement, then 2nd line is executing total number of times as expected, and 3rd is not working, and so on.

This is the output, when all the statement are uncommented

this is the output, when all the statement are uncommented

This is the output, when first line is commented, and two lines are uncommented

this is the output, when first line is commented, and two lines are uncommented

This is the output, when first and second line is commented, and last line are uncommented

this is the output, when first and second line is commented, and last line are uncommented

This function displays output from edit-submodule.component.ts:863 in the screenshots.

Upvotes: 0

Views: 78

Answers (1)

Shameen
Shameen

Reputation: 3268

Youre missing the braces in the for loop, so it's currently parsing this as:

for (var item in items) {
  console.log(item);
}
console.log(items[item]);
console.log(typeof(items[item]));

Upvotes: 2

Related Questions