0

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

9
  • Just get rid of the alert. If you're looking for Freddie, you'll alert "user not found" when the loop looks at Herbie and Wayne before him. And use break when you find the user you want. Commented Aug 26, 2014 at 20:11
  • 1
    Also, use a for loop to loop arrays, not a for..in. Commented Aug 26, 2014 at 20:12
  • 1
    Don't use for ... in loops for iterating through arrays in JavaScript. Use a plain for loop and an index variable to explicitly count through the properties, or else use the .forEach() method. Commented Aug 26, 2014 at 20:12
  • @Barmar Thank you but actually I am getting the selected user :) problem I'm trying to solve is getting all of the data in all of the (date & time) objects for the sel user. Thanks for the tip to use break. Commented Aug 26, 2014 at 20:15
  • 1
    If the keys are really unique, date & time should be a single object, not an array of objects with one property each. Then for (var someKey in datesAndTimesArray){ would work just fine. Commented Aug 26, 2014 at 20:15

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks again. You and Felix have confirmed my suspicion as to how I'm formatting my data (ergo the "JSON newb asking", and having formatted it more than a few ways already). Also I appreciate the heads-up on my if statement; I was a little doubtful of that too but it was back-burnered since it wasn't preventing me from working on getting the data ;)
p.s. using if (usersArray[user].userName != selUser) { alert("user not found"); } as you sugg above, I'm getting error for all selections
That's what I said would happen, which is why you shouldn't do that test.
if you want to know if the user isn't found, check at the end of the loop to see if datesAndTimesArray is set.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.