Hi All - Im looking into getting the JSON formatted data on the for loop and console.log it
The data from the json response is inside an array, and there is an object that contains the properties that I need to access.
Something like this:
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4) {
let toBeFormated = xmlHttp.responseText;
let formatted = JSON.parse(toBeFormated);
//console.log(formatted.data);
for (let i = 0; i < formatted.data.length; i++) {
console.log(formatted.data.length[i]);
}
}
}
is this a FCC lesson?
Is it required to use the old approach?
You could use fetch instead of xmlHttp.
So your array has (multiple) objects. You access them with their index.
Then you can read a specific property of the object with either dot notation (e.g. .id) or square brackets (e.g. ["id"]). There are lessons on FCC about this.
Hi - Its not from a lesson. I review the lesson for “Accessing Object Properties with Dot Notation” though.
In the lesson, i understand I can access the object properties calling the objectName.property (with the dot or the bracket). However, from the formatted json data that I got from XHR, i can’t seem to understand where is the array name or the proper way to access it.
Also, I think my loop approach is not correct. Looking at similar requests, I saw that the best way will be to loop thru the array, which makes sense. So this where I’m at.