2

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
  • There's only one entry in myObject, and its property name is "records". Commented Nov 10, 2020 at 17:50
  • index is an index into myObject properties, why are you using it as an index for value? Commented Nov 10, 2020 at 17:55
  • How can I change this code to iterate every "CustomerName". Commented Nov 10, 2020 at 17:55

3 Answers 3

2

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}`);
});

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

Comments

0

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)
   })
}

Comments

0

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.

2 Comments

myObject is not defined
Run the snippet now

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.