Traversing Through Object + Array + Object

MarioD

Get guys,

I've tried looking through a bunch of tutorials but I can't seem to find one that covers this in detail. Maybe somebody could point me int he right direction.

I have a .json file that looks like this:

{
    "users": [{
        "firstName": "John",
        "lastName": "Doe"
    }, {
        "firstName": "Sabrina",
        "lastName": "Doe"
    }]
}

I want to run a .each loop and grab all of the users. Here's what I've tried:

$.getJSON("database.json", function(data) {
    $.each(data.users, function(key, val) {
        $('.dbUL').append('<li>' + key + ' : ' + val + '</li>');
    });
});

So it looks like it actually does spit out the array since I get an output of 0 : [object Object] and 1 : [object Object].

My question is, how do I dig into the array and actually spit out the objects that I have stored in my array?

Roko C. Buljan

Knowing the properties what you want get (fistName, lastName): jsBin

$.getJSON("database.json", function(data) {
    $.each(data.users, function(idx, val) {
        $('.dbUL').append('<li>' + val.firstName + ' : ' + val.lastName + '</li>');
    });
});

Not knowing the properties you want to get: jsBin

$.getJSON("database.json", function(data) {

    var LIhtml = "";

    $.each(data.users, function(idx, obj) { 

      LIhtml += "<li>";

      for(var key in obj){
        if(obj.hasOwnProperty(key)){
          LIhtml += obj[key]+" ";
        }
      } 

      LIhtml += '</li>';

    });

    $('.dbUL').append(LIhtml);
});

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Animating an object through an array of points on Canvas while maintaining a uniform speed?

분류에서Dev

Pass object through function

분류에서Dev

Passing object through segue

분류에서Dev

JavaScript--merge two array objects into one arr object then loop through each index/object

분류에서Dev

How to get array object in object

분류에서Dev

Accessing object in array recreating object?

분류에서Dev

Object array to generic array

분류에서Dev

iterating through object of objects with condition

분류에서Dev

Looping through XML Object PHP

분류에서Dev

Accessing a member function through an object

분류에서Dev

Array position by object value

분류에서Dev

Add PSVariable object to array

분류에서Dev

Looping an object with an string array

분류에서Dev

Update specific object in array

분류에서Dev

can not access the array of an object

분류에서Dev

JS fill Object as an array

분류에서Dev

Pass an object into an array in JavaScript

분류에서Dev

php array not saved in object

분류에서Dev

Remove object from array

분류에서Dev

Sorting array object in php

분류에서Dev

Object and array with self referencing

분류에서Dev

function to exclude array object

분류에서Dev

How to populate an object inside an object in an array?

분류에서Dev

Ionic / Angular Pushing Object to Array Object

분류에서Dev

Keep getting [object, Object] from array for $.each

분류에서Dev

assigning new value to an object in object array in jquery

분류에서Dev

Iterating through object with different defined patterns

분류에서Dev

Iterate through json object in c#

분류에서Dev

Java, call object methods through arraylist