1
public Update() {

         this.Data = this.Items;
         console.log(this.Data);
         for (let value of this.Data) {
             console.log(value);
         }
     }

console

[Object, Object, Object]

Object
CandidateName:"B"
ControlId:0
CreatedBy:null
CreationDateTime:null
ExamId1:2000
ExamName:" Safety"
Id:1292353

after last object it showing length:3

when i going to loop over this object,it is throwing error length is undefined,please help me.

3
  • What do you want? Commented May 25, 2017 at 10:53
  • Do you want to iterate over array of objects or a single object in the array? Commented May 25, 2017 at 11:00
  • @Mr_Perfect iterate over every single object in that array Commented May 25, 2017 at 11:07

3 Answers 3

2

If I understand correctly, this.Items is probably undefined in some cases and you cannot iterate.

So:

for (let value of (this.Data || [])) {

This guards against bad values

Sign up to request clarification or add additional context in comments.

2 Comments

this.Items contain that object arrayand length,length is total number of object.
this.Data = this.Items in your case, so same thing
1

The for in or for of statement should be avoided for iterating over arrays. It has two "drawbacks":

1) The order is not guarantee

2) Also inherited properties will be listed/enumerated, if enumerable:false is not specified when defining the property.

If for example you add a property to your prototype, this loop will iterate also over that one.

Array.prototype.test = "test";
var a = ['a', 'b'];
for (let i in a) {
  console.log(a[i]);
}
for (let i of a) {
  console.log(i);
}

You should see also your property printed.

If you change your loop into a sequential for loop:

for (let i = 0; i < this.Data.length; i++ value of this.Data) {
  console.log(this.Data[i]);
}

or a:

this.Data.forEach((el) => {
  console.log(el);
});

you may not see your issue.

Comments

0

If you want to iterate over objects, You must use Object.keys(your_obj). Because object doesn't have length property. You can iterate through only of type 'Array' or 'String' using for of. You can use Object.keys(your_obj).length for sequential for loop for(var i=0; i<Object.keys(your_obj).length; i++)

public Update() {

         this.Data = this.Items;
         console.log(this.Data);
         for (let obj of this.Data) {
             console.log(obj);
             //iterate over object here
             for(let property of Object.keys(obj)){
               // write your logic for specific object
             }
         }
     }

Extension to quirimmo's answer, sequential for loop, use this

     this.Data.forEach(function(obj){
         console.log(obj);
         //iterate over object here
         for(var i=0; i<Object.keys(obj).length; i++){
           // write your logic for specific object
         }
     })

1 Comment

that means this.Data is undefined. So, the problem is somewhere else. What is this.Items?

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.