0

I have this data response from an AJAX call:

{"18:00":{"twopersons":1,"fourpersons":0}}

Which gets stored into a variable by statsarray = data;

Now how can i loop through statsarray and output the twopersons value?

So I can alert:

18:00 - There's 2 x 2persons and 0 x 4persons

Here is the Ajax call:

var statsarray;
var currentloopeddate = test_date.toString('yyyy-MM-dd')
$.post("/home/sessions",
    { action: 'partner_calendar_checkseats', date: currentloopeddate },
    function(data) { statsarray = data; }
);
7
  • 1
    Loop statsarray? or rather access twopersons? Commented Aug 8, 2012 at 11:20
  • You have a pretty good reputation. Have you tried something? Commented Aug 8, 2012 at 11:20
  • Please provide the code that makes the Ajax call and show how the variable data is initialized. Commented Aug 8, 2012 at 11:36
  • @JohnWatts Updated with ajax call Commented Aug 8, 2012 at 11:40
  • The success callback is only called if the request succeeds. I'm guessing it failed. Try adding an error()callback for debugging. Commented Aug 8, 2012 at 11:47

3 Answers 3

2

Just do the following:

var twopersons = data["18:00"].twopersons;
var fourpersons = data["18:00"]["fourpersons"];

(Both variants are possible)

A variant would be:

var shorter = data["18:00"];
var twopersons = data.twopersons;
// ...
Sign up to request clarification or add additional context in comments.

6 Comments

Didn't workout Uncaught TypeError: Cannot read property '18:00' of undefined
Updated question with ajax call
Then replace response with data, of course you cannot simply use copy and paste...
No ofcourse I have not just copy paste. I think its my statsarray that doesnt get set with the data from ajax call response, but I cant see where it goes wrong?
@Karem see my answer (the last updated part) because that may be the reason why your variable is undefined when you try to use it.
|
1

Something like:

var tst = {"18:00":{"twopersons":1,"fourpersons":0}};
for(k in tst) {
  for(var z in tst[k]) {
   console.log(k + ": Theres "+tst[k][z] + " X " + z);
  }
}

2 Comments

No need for minification ;)
I would add .hasOwnProperty checks to make sure it won't break with libraries that extend Object.prototype (see my answer)
0

You can try something like this:

(UPDATE: better example)

var statsarray = {"18:00":{"twopersons":1,"fourpersons":0}};

var hour, persons, line, array;
for (hour in statsarray) {
    if (statsarray.hasOwnProperty(hour)) {
        array = [];
        for (persons in statsarray[hour]) {
            if (statsarray[hour].hasOwnProperty(persons)) {
                array.push(statsarray[hour][persons] + " x " + persons);
            }
        }
        line = hour + " - There's " + array.join(' and ');
        alert(line);
    }
}

See: DEMO.

Unfortunately you have to test with .hasOwnProperty to make sure it will work with some libraries.

UPDATE: You have added the code from your AJAX call in your question and I noticed that you declare the statsarray variable outside the callback function, but assign some value to that variable inside the callback. Just keep in mind that you have to run your iteration code inside the function that is the AJAX callback, where you have: statsarray = data; - just after this line, to make sure that you actually have some values to iterate over.

Comments

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.