up vote 5 down vote favorite
2
share [g+] share [fb]

Basically I have the following JSON-originated Object:

({
    "id" : 3,
    "clientName" : "Avia",
    "monthlyactiveusers" : 2083,
    "dailynewlikes" : 0,
    "totallikes" : 4258,
    "usersgraph" : {
        "sTotalLikes" : [{
            "likes" : 79,
            "date" : "1/1/2010"
        },
        {
            "likes" : 116,
            "date" : "1/1/2010"
        }],
        "sDailyActiveUsers" : [{
            "likes" : 79,
            "date" : "1/1/2010"
        },
        {
            "likes" : 116,
            "date" : "1/1/2010"
        }]
    }
});

And I need the following result:

sTotalLikes = [['1/1/2010', 79],['1/1/2010', 79],['1/11/2010', 79]];
sDailyActiveUsers = [['1/1/2010', 10],['1/5/2010', 300],['1/11/2010', 220]];

I know you can iterate through the object to build the array using the following code but I couldn't figure out how to build the JavaScript array itself. Thanks in advance for help.

var sTotalLikes = new Array();

 for (var i = 0; i < usersgraph.sTotalLikes.length; i++) {
    //how do I build the arry ?
    sTotalLikes[i]
  }
link|improve this question

80% accept rate
Makes me wonder if there's some kind of oddball pivot utility in existence for JavaScript data structures. That would be useful. – John K Dec 18 '10 at 1:44
3  
... it is an object literal, not an JSON object (although it might have been a JSON object when it was in a string). Luckily I bookmarked this URL now: benalman.com/news/2010/03/theres-no-such-thing-as-a-json – Felix Kling Dec 18 '10 at 1:51
@Felix: yes, +1 - a JS object with quoted keys a "JSON object" do not make. – MДΓΓ БДLL Dec 18 '10 at 2:02
feedback

4 Answers

up vote 2 down vote accepted

You'll have to Iteration through each item in sTotalLikes and sDailyActiveUsers.

You can also see the live demo here for complete and working program with comments. :)

// declare arrays for storing total likes and active users
var totalLikes = [];
var activeUsers = [];



// first iterate for total likes
for (var i = 0; i < data.usersgraph.sTotalLikes.length; i ++)
{
    var like = data.usersgraph.sTotalLikes[i];

    // create a new array of date and likes
    // and push into total likes
    totalLikes.push([like.date, like.likes]);
}



// then iterate for active users
for (var i = 0; i < data.usersgraph.sDailyActiveUsers.length; i ++)
{
    var user = data.usersgraph.sDailyActiveUsers[i];

    // create a new array of date and likes
    // and push into active users
    activeUsers.push([user.date, user.likes]);
}

hope this helps!

link|improve this answer
+1 for good detail – Singleton Dec 18 '10 at 20:57
Wow!! jsfiddle.net is awesome. Thanks for pointing this out. It's exactly what I needed to test my staff.I'm using jqplot and originally I was trying to feed it with the Json string specified above but for some reason it doesn't like it so I'll just build a JavaScript array out of it. Thanks again for the help. – Vasile Laur Dec 19 '10 at 3:25
Always welcome :) – Zain Shaikh Dec 19 '10 at 8:14
feedback

Try this.. you can easily extend it for sDailyActiveUsers

var sTotalLikes = new Array();
    var lsTotalLikes = usersgraph.sTotalLikes;
 for (var i = 0; i < lsTotalLikes.length; i++) 
 {

    var obj = lsTotalLikes[i];
        var lArr = []
        lArr.push(obj.date);
        lArr.push(obj.likes);
        sTotalLikes.push(lArr) 
  }
link|improve this answer
1  
Prefer var sTotalLikes = []; to the one you have in the code. – Thrustmaster Dec 18 '10 at 4:34
1  
:) I didn't want to change the initial code that OP had, you can look at lArr definition, I have used [], cos I am lazy to type :) – Cybernate Dec 18 '10 at 4:37
feedback

It looks to me like you just want to look at the values of the objects.

var usersgraph = { ... }; // pulled from the data in your question
var result = {};
for (users_key in usersgraph) {
    var vals = [];
    var data = usersgraph[users_key]
    for (k in data) {
        vals.push(values(data[k]));
        // or if you need to order them differently..
        //vals.push([ data[k]['date'], data[k]['likes'] ]);
    }
    result[users_key] = vals;
}

Oh, if you had not guessed already you can use [] to create an array and {} to create an object/associative array.

link|improve this answer
feedback

Like this (referring to your code):

/* inside your for loop */
sTotalLikes.push([
    usersgraph.sTotalLikes[i].date,
    usersgraph.sTotalLikes[i].likes
])
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.