-1

I imported an array structure via JSON and parsed it. But I can't access it via index or via forEach. I put out a log of the array, which seems fine, but when I try to log the first element of the first element via array[0][0] or capsuled for loops it says undefined. I guess that I somehow messed up my formats, but can't tell.

How may I access the object inside of the arrays without changing the array structure?

Array structure:


    [            // Root array (result)
      [          // result[0] works fine
        [        // result[0][0] = undefined (?)
          {      // object I need to access
            "ARBPL": "616947  ",
            "VDATU": "20210301",
            "BDATU": "20210327",
            "HALLE": "0001                          ",
            "PLATZ": "002                           ",
            "SUBAB": "FW                            ",
            "ABTEI": "FW                            ",
            "T_SCHNEIDP": []
          }
        ]
      ]
    ]


Output via Javascript:


    // ... this.responseText is from a function, which works just fine.
    function getArray(address, fromDate, toDate) {
            var linkC = "linkToGetArray" + address + "/" + fromDate + "/" + toDate;
            var xmlhttpR = new XMLHttpRequest();
            var arr = [];
            xmlhttpR.onreadystatechange = function () {
                if (this.readyState == 4 && this.status == 200) {
                    arr.push(JSON.parse(this.responseText));
                }
            };
            xmlhttpR.open("GET", linkC, true);
            xmlhttpR.send();
            return arr;
        }

    var result = [];
    result = getArray(address, fromDate, toDate);

    // log of array (works fine)
    console.log("result: ");
    console.log(result);
    
    // log of first array element (works fine)
    console.log("result[0]: ");
    console.log(result[0]);
    
    // log of first element of first element (doesn't work, output: undefined)
    console.log("result[0][0]: ");
    console.log(result[0][0]);
   
    // tried this approach as well, lead to:
    // Uncaught TypeError: result[Symbol.iterator().next().value[Symbol.iterator]().next().value is undefined 
    const [[[obj]]] = result;

I made a screenshot of my browser console to show the array construct and the output from above.

enter image description here

ecco
  • 181
  • 14
  • 2
    `XMLHttpRequest` is **asynchronous** . The function returns an empty array that gets populated after you are trying to access it. The console shows updates made to that array as a live object even after you log it See: [Weird behavior with objects & console.log](https://stackoverflow.com/questions/23429203/weird-behavior-with-objects-console-log) – charlietfl Mar 27 '21 at 01:35
  • Also strange that you are pushing the result array into an empty array. Seems pointless – charlietfl Mar 27 '21 at 01:38

1 Answers1

0

You gonna go inside 3 level deep to access the object.

const arr = [
  // Root array (result)
  [
    // result[0] works fine
    [
      // result[0][0] = undefined (?)
      {
        // object I need to access
        ARBPL: "616947  ",
        VDATU: "20210301",
        BDATU: "20210327",
        HALLE: "0001                          ",
        PLATZ: "002                           ",
        SUBAB: "FW                            ",
        ABTEI: "FW                            ",
        T_SCHNEIDP: [],
      },
    ],
  ],
];

const obj = arr[0][0][0];
console.log(obj);

You can also use array destructuring, use [] as many as in source array

const arr = [
  // Root array (result)
  [
    // result[0] works fine
    [
      // result[0][0] = undefined (?)
      {
        // object I need to access
        ARBPL: "616947  ",
        VDATU: "20210301",
        BDATU: "20210327",
        HALLE: "0001                          ",
        PLATZ: "002                           ",
        SUBAB: "FW                            ",
        ABTEI: "FW                            ",
        T_SCHNEIDP: [],
      },
    ],
  ],
];

const [[[obj]]] = arr;
console.log(obj);
Accessing the array element individually

const arr = [
  // Root array (result)
  [
    // result[0] works fine
    [
      // result[0][0] = undefined (?)
      {
        // object I need to access
        ARBPL: "616947  ",
        VDATU: "20210301",
        BDATU: "20210327",
        HALLE: "0001                          ",
        PLATZ: "002                           ",
        SUBAB: "FW                            ",
        ABTEI: "FW                            ",
        T_SCHNEIDP: [],
      },
    ],
  ],
];

console.log("arr")
console.log(arr);

console.log("arr[0]")
console.log(arr[0]);

console.log("arr[0][0]")
console.log(arr[0][0]);

console.log("arr[0][0][0]");
console.log(arr[0][0][0]);
DecPK
  • 24,537
  • 6
  • 26
  • 42