0

This is a return from an API call in JSON format. I know how to access the keys like "id", "name", "phone". But when it comes to accessing the array under "eldSettings", or the Objects keys under "tags", I'm having trouble. Currently I can loop though and console log "id", "name", "phone", and etc.

const driveArry = dataobj.data;
let tag = "";
for (let x in driveArry) {
  tag += driveArry[x].name + "<br>";
}
console.log(tag);

JSON Return

"data":[
{
    "id": "100",
    "name": "Jon Wilson",
    "username": "JWil",
    "phone": "5555555555",
    "eldSettings": {
        "rulesets": [
            {
                "cycle": "USA 70 hour / 8 day",
                "shift": "US Interstate Property",
                "restart": "34-hour Restart",
                "break": "Property (off-duty/sleeper)"
            }
        ]
    },
    "timezone": "America/New_York",
    "updatedAtTime": "2020-11-19T18:54:39.069917Z",
    "createdAtTime": "2020-02-25T21:20:05.740835Z",
    "carrierSettings": {
        "carrierName": "",
        "mainOfficeAddress": "",
        "dotNumber": 0,
        "homeTerminalName": "",
        "homeTerminalAddress": ""
    },
    "driverActivationStatus": "active",
    "tags": [
        {
            "id": "730",
            "name": "Ocean Bay",
            "parentTagId": "728"
        },
        {
            "id": "1776",
            "name": "Operations Unit",
            "parentTagId": "14731"
        },
        {
            "id": "2480",
            "name": "Foremen",
            "parentTagId": "1232"
        },
        {
            "id": "137",
            "name": "Ocean Bay Field Foreman 1",
            "parentTagId": "1860"
        },
        {
            "id": "1435",
            "name": "Ocean Bay Pickups",
            "parentTagId": "1223"
        }
    ],
    "staticAssignedVehicle": {
        "id": "8081719",
        "name": "1-22"
    },
    "vehicleGroupTag": {
        "id": "1137",
        "name": "Ocean Bay Field Foreman 1",
        "parentTagId": "1230"
    }
},
Barmar
  • 741,623
  • 53
  • 500
  • 612
TXjayhawk
  • 1
  • 1
  • 1
    `driveArry[x].eldSettings.rulesets[y].cycle` – Barmar Sep 28 '22 at 20:20
  • 1
    `driveArry[x].tags[z].id` – Barmar Sep 28 '22 at 20:21
  • Use [`for...of`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of) instead of `for...in` to loop over arrays. [Here's why](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in#array_iteration_and_for...in). – Emiel Zuurbier Sep 28 '22 at 20:22

0 Answers0