How to iterate index in Javascript using forEach loop

Viewed 69

I am trying to iterate this object. I have checked code over the internet and found that Object.enteries can work but so far it is only returning 1st CustomerName i.e. "Asad". I want to iterate it to the complete array not just the first one.

var myObject = {
        "records": [
            {
                "id": "recDBqsW7C3Dk3HMd",
                "fields": {
                    "Latitude": "24.898907",
                    "Longitude": "67.117303",
                    "CustomerName": "Asad"
                },
                "createdTime": "2020-10-07T04:43:31.000Z"
            },
            {
                "id": "recGlfTbUcEvP46Lf",
                "fields": {
                    "Latitude": "24.907641",
                    "Longitude": "67.1088035",
                    "CustomerName": "Umar"
                },
                "createdTime": "2020-10-07T04:44:11.000Z"
            },
            {
                "id": "recfsQsoznDyWPDe8",
                "fields": {
                    "Latitude": "24.911112",
                    "Longitude": "67.105117",
                    "CustomerName": "Ali"
                },
                "createdTime": "2020-10-06T09:11:05.000Z"
            }
        ]
    };
    
    
    Object.entries(myObject).forEach(([key, value], index) => {
      console.log( value[index].fields.CustomerName); // key ,value
    });

Result:

"Asad"
3 Answers

The array is actually a property of the object literal you defined it in. All you need to do is access that array and call Array.prototype.forEach on it in order to access it's elements. Something like this:

//Extract the array from your data structure into a variable. Always use 
//a const if the variable will not need to be re-assigned in the program.
const {
  records
} = {
  "records": [{
      "id": "recDBqsW7C3Dk3HMd",
      "fields": {
        "Latitude": "24.898907",
        "Longitude": "67.117303",
        "CustomerName": "Asad"
      },
      "createdTime": "2020-10-07T04:43:31.000Z"
    },
    {
      "id": "recGlfTbUcEvP46Lf",
      "fields": {
        "Latitude": "24.907641",
        "Longitude": "67.1088035",
        "CustomerName": "Umar"
      },
      "createdTime": "2020-10-07T04:44:11.000Z"
    },
    {
      "id": "recfsQsoznDyWPDe8",
      "fields": {
        "Latitude": "24.911112",
        "Longitude": "67.105117",
        "CustomerName": "Ali"
      },
      "createdTime": "2020-10-06T09:11:05.000Z"
    }
  ]
};

//Iterate over the items in the array to access the data you need
records.forEach(record => {
  console.log(`${record.id} = ${record.fields.CustomerName}`);
});

In general, you are using the wrong approach here.

The thing that you need to do is:

var myObject = {
        "records": [
            {
                "id": "recDBqsW7C3Dk3HMd",
                "fields": {
                    "Latitude": "24.898907",
                    "Longitude": "67.117303",
                    "CustomerName": "Asad"
                },
                "createdTime": "2020-10-07T04:43:31.000Z"
            },
            {
                "id": "recGlfTbUcEvP46Lf",
                "fields": {
                    "Latitude": "24.907641",
                    "Longitude": "67.1088035",
                    "CustomerName": "Umar"
                },
                "createdTime": "2020-10-07T04:44:11.000Z"
            },
            {
                "id": "recfsQsoznDyWPDe8",
                "fields": {
                    "Latitude": "24.911112",
                    "Longitude": "67.105117",
                    "CustomerName": "Ali"
                },
                "createdTime": "2020-10-06T09:11:05.000Z"
            }
        ]
    };

myObject.records.forEach((value, index) => {
      console.log( value.fields.CustomerName); // key ,value
 });

By doing so you will iterate through your records easily and get all names.

What you currently are doing is iterating trough the key-value pairs of the myObject where Object.entries(myObject) will return to you [['records', [array of the users]]] then the console.log in the forEach block will be called once, as you have only one entry there.

Try this code please. It finds the keys and then iterates with forEach

for (var key of Object.keys(myObject)) {
   myObject[key].forEach((v) => {
     console.log(v.fields.CustomerName)
   })
}
Related