Blue Lovag
Blue Lovag

Reputation: 783

Getting inside the entries and keys of objects or objects of objects

I just started dealing with objects and i have no clue how i´m getting inside those nested objects. Let me show you how i tried:

  $.ajax({
        dataType: "json",
        url: "Bibliothek.json",
        data: data,
        success: function(response) {
            console.log(Object.entries(response,"objects"));
            console.log(Object.values(response,"values"));

        var entry1 = Object.entries(response,"objects");
        var entry2 = Object.entries(entry1,"entry1");
        var entry3 = Object.entries(entry2,"entry2");

        console.log(entry1,"entry1");
        console.log(entry2,"entry2");
        console.log(entry3,"entry3");

        var mainkey = Object.values(response);
        var types = Object.values(mainkey);
        var files = Object.values(types);

        console.log(mainkey,"mainkey");
        console.log(types,"types");
        console.log(files,"files");

        },
    });

This is what i get, so how do i get into Attribute:Array(1) for example?

console

The first repsonse from the success looks like that:

enter image description here

EDIT: The response log + JSON file im reading: https://pastebin.com/wiqajA4P

enter image description here

Upvotes: 0

Views: 53

Answers (1)

Richard Price
Richard Price

Reputation: 490

In order to access any of the entities which are in Arrays you need to reference their index (0,1,2,3) etc. With object properties, you can access these via their object key.

So looking at your attached image, which property are you attempting to access?

The first object is located at Geraetewelten, so can be accessed using the dot notation

response.Geraetewelten

You can continue to follow this to reach the Atrribute array (Listed as Array(1) meaning it has 1 child.

as this is an array you can access the child by index starting at 0

so this would be

response.Geraetewelten.Atrribute[0]

etc.

Upvotes: 1

Related Questions