Getting all keys & their values from nested objects in JSON array (w/out jQuery)

user1613163

JSON newb asking: Given the following json data:

{
    "Users": [
        {
            "userName": "Herbie",
            "dates & times": [
                { "2014.08.01": "120.0" },
                { "2014.08.02": "123.0" },
                { "2014.08.03": "126.0" }
            ]
        },
        {
            "userName": "Wayne",
            "dates & times": [
                { "2014.08.01": "120.0" },
                { "2014.08.02": "123.0" },
                { "2014.08.03": "126.0" }
            ]
        },
        {
            "userName": "Freddie",
            "dates & times": [
                { "2014.08.01": "120.0" },
                { "2014.08.02": "123.0" },
                { "2014.08.03": "126.0" }
            ]
        },
        {
            "userName": "Ron",
            "dates & times": [
                { "2014.08.01": "120.0" },
                { "2014.08.02": "123.0" },
                { "2014.08.03": "126.0" }
            ]
        },
        {
            "userName": "Tony",
            "dates & times": [
                { "2014.08.01": "120.0" },
                { "2014.08.02": "123.0" },
                { "2014.08.03": "126.0" }
            ]
        }
    ]
}

... and wanting to retrieve a specified user's dates & times data so it can be rendered, e.g.:

Date 01: Time 01
Date 02: Time 02
Date 03: Time 03

I've read much about looping thru arrays and objects but I'm still confused as to how to get the dates & times (or any keys/values) if the keys are unique. So far I have this:

var usersArray = myObj["Users"];
for (var user in usersArray){
    if (!usersArray[user].userName == selUser) {
        alert("user not found");
    }
    else if (usersArray[user].userName == selUser) {
        var datesAndTimesArray = usersArray[user]["dates & times"];
        console.log(selUser +"'s dates & times: " + datesAndTimesArray); // returns array of objects
    }
}

for (var someKey in datesAndTimesArray){
    // here's where I'm struggling, trying to get all the dates & times for the selected user, such that I can render it as described above
    // pseudo-code
}   

Btw: studying javascript, not using jquery etc.

Many thanks in advance,

svs

Barmar

Since datesAndTimesArray is an array of objects, you need a nested loop to process it.

for (var i in datesAndTimesArray) {
    var obj = datesAndTimesArray[i];
    for (var date in obj) {
        console.log('Date ' + date + ': Time' + obj[date]);
    }
}

It would probably be better for the dates and times to be single objects instead of arrays, e.g.

        "dates & times": {
            "2014.08.01": "120.0",
            "2014.08.02": "123.0",
            "2014.08.03": "126.0"
        }

Then you only need to do for (date in datesAndTimesArray)

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

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

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

jquery $.each nested in another $each. getting data from a JSON

분류에서Dev

Cannot getting data from external nested json file form jquery

분류에서Dev

jQuery - How to get JSON array name from nested JSON

분류에서Dev

nested ListView not getting values from parent listview

분류에서Dev

Print out specific values from a JSON file (nested objects-arrays) w/ JAVA

분류에서Dev

how to alert each values from json array in jquery

분류에서Dev

How to get all values of objects inside array

분류에서Dev

Create a union from values of a nested object with different keys

분류에서Dev

transform JSON array of objects, create new property from two property values

분류에서Dev

How to get all the keys values from Redis Cache in C#?

분류에서Dev

Assign Nested Keys and Values in Dictionaries

분류에서Dev

getting incorrect values while fetching values from an array

분류에서Dev

Make json from array of objects javascript

분류에서Dev

Erlang, Nested JSON objects

분류에서Dev

jQuery - Select elements by value from array of values

분류에서Dev

How to avoid nested keys and values while joining tables and using 'for json auto'

분류에서Dev

Jquery Get specific values from JSON result?

분류에서Dev

Jquery Autocomplete from json list all elements

분류에서Dev

Move values from JSON to javascript array

분류에서Dev

Retrieving values from a multidimensional JSON array

분류에서Dev

JavaScript: Add JSON objects from one array to another array conditionally

분류에서Dev

Error when setting and getting values from my two dimensional array

분류에서Dev

Getting variable values from variable names listed in array in Bash

분류에서Dev

Trouble getting json_encode array elements into seperate jQuery variables

분류에서Dev

How to get JSON response array all index values?

분류에서Dev

how to sum up all values with same keys in an object inside an array and get their average

분류에서Dev

Ruby on Rails - Get array of values from array of hash with particular order of existing keys

분류에서Dev

PointlnPolygon function not processing multiple objects from JSON Array

분류에서Dev

Create nested objects from CSV

Related 관련 기사

  1. 1

    jquery $.each nested in another $each. getting data from a JSON

  2. 2

    Cannot getting data from external nested json file form jquery

  3. 3

    jQuery - How to get JSON array name from nested JSON

  4. 4

    nested ListView not getting values from parent listview

  5. 5

    Print out specific values from a JSON file (nested objects-arrays) w/ JAVA

  6. 6

    how to alert each values from json array in jquery

  7. 7

    How to get all values of objects inside array

  8. 8

    Create a union from values of a nested object with different keys

  9. 9

    transform JSON array of objects, create new property from two property values

  10. 10

    How to get all the keys values from Redis Cache in C#?

  11. 11

    Assign Nested Keys and Values in Dictionaries

  12. 12

    getting incorrect values while fetching values from an array

  13. 13

    Make json from array of objects javascript

  14. 14

    Erlang, Nested JSON objects

  15. 15

    jQuery - Select elements by value from array of values

  16. 16

    How to avoid nested keys and values while joining tables and using 'for json auto'

  17. 17

    Jquery Get specific values from JSON result?

  18. 18

    Jquery Autocomplete from json list all elements

  19. 19

    Move values from JSON to javascript array

  20. 20

    Retrieving values from a multidimensional JSON array

  21. 21

    JavaScript: Add JSON objects from one array to another array conditionally

  22. 22

    Error when setting and getting values from my two dimensional array

  23. 23

    Getting variable values from variable names listed in array in Bash

  24. 24

    Trouble getting json_encode array elements into seperate jQuery variables

  25. 25

    How to get JSON response array all index values?

  26. 26

    how to sum up all values with same keys in an object inside an array and get their average

  27. 27

    Ruby on Rails - Get array of values from array of hash with particular order of existing keys

  28. 28

    PointlnPolygon function not processing multiple objects from JSON Array

  29. 29

    Create nested objects from CSV

뜨겁다태그

보관