views:

52

answers:

4

I have the following javascript which I need to make dynamic.

data.addRows(2);
data.setValue(0, 0, 'name goes here');
data.setValue(0, 1, value 1 goes here); 
data.setValue(0, 2, value 2 goes here); 
data.setValue(1, 0, 'name goes here');
data.setValue(1, 1, value 1 goes here); 
data.setValue(1, 2, value 2 goes here); 

I guess a loop is the best way to go forward:

I have come up with the following json structure:

[ 
    {"User1":{"value1": 50.00,"value2": "100"}}, 
    {"User2":{"value1": 10.00,"value2": "20"}}, 
    {"User3":{"value1": 10.00,"value2": "20"}}, 
    {"User4":{"value1": 10.00,"value2": "20"}}, 
    {"User5":{"value1": 20.00,"value2": "40"}} 
] 

I think this structure needs improving. Can someone please suggest a better structure to make it very easy for me to extract the data I want to extract from this?

+2  A: 

I don't see that you could make it anymore simple, unless there's something special about the data that would allow repeated parts to be separated out in some fashion.

AHungerArtist
The reason why I am asking, is because I am having difficulty trying to loop through the data to make the javascript above dynamic.
oshirowanen
The reason you are having difficulty is because it looks like you are using a unique key (user1,user2,user3) as the key for the data associated. By creating uid and setting it to user1, now you have a single way of referencing any user.
Angelo R.
+1  A: 

Try something like:

{"users": [
    {"value1": 50, "value2": 100},
    {"value1": 10, "value2": 20},
    {"value1": 10, "value2": 20},
    {"value1": 10, "value2": 20},
    {"value1": 20, "value2": 40}
]}

Or, if you want to have your users have IDs:

{"users": {
    "userId1": {"value1": 50, "value2": 100},
    "userId2": {"value1": 10, "value2": 20},
    "userId3": {"value1": 10, "value2": 20},
    "userId4": {"value1": 10, "value2": 20},
    "userId5": {"value1": 20, "value2": 40}
}}

Remember, all numbers in JavaScript are floating-point numbers.

Anthony Mills
This JSON wouldn't be very helpful because you can't tie the values to any specific user.
Brian Ray
Each value pair is for a specific user, I don't think this will work.
oshirowanen
In your second example, does it become easier to loop through that to stick the data into the javascript example I gave above?
oshirowanen
Edited to add IDs. With this, you can access it either sequentially (`for (var user in data.users) { ... }`) or randomly (`data.users["userId1"].value1`).
Anthony Mills
If all you want to do is loop through it sequentially, Angelo R.'s solution works well, and it has the added advantage of an easily-accessible `length`. But mine allows both forms of access easily.
Anthony Mills
I'm not having much luck looping through Angelo R.'s example.
oshirowanen
+4  A: 
var dataset = [
  {uid: 'user1', value1: 50.00, value2: 100},
  {uid: 'user2', value1: 10.00, value2: 20}, 
];

That way you can do data.length to figure out how many values you have and you can loop a bit easier. For example:

for(i = 0; i < dataset.length; i++) {
   console.log(data[i].uid);
}

Using your example:

data.addRows(2); 
var l = dataset.length, i, x = 0, y = 0;
for(i = 0; i < l; i++) {
  data.setValue(y, x, dataset[i].uid); x++;
  data.setValue(y, x, dataset[i].value1); x++;
  data.setValue(y, x, dataset[i].value2); x=0;
  y++;
}
Angelo R.
I have made the necessary changes to the JSON structure, but can't seem to loop through it to get it into the javascript above.
oshirowanen
I've modified my answer to show you one possible solution. It's by no means the best way of keeping track of x and y, but it should work fine for what you need.
Angelo R.
Seems to be working so far! Testing it out right now. Thanks
oshirowanen
A: 

I think you can try something like this, the idea is from ColdFusion SerializeJson()

I assume your data is like a table

user        value1        value2
------------------------------------
user1       50            100
user2       10            20
...

the json structure is

{"COLUMNS":["USER","VALUE1","VALUE2"],"DATA":[["user1",50.0,100.0],["user2",10.0,20.0]]} 

more clear reading

{
    "COLUMNS":
        [
            "USER",
            "VALUE1",
            "VALUE2"
        ],
    "DATA":
        [
            [
                "user1",
                50.0,
                100.0
            ],
            [
                "user2",
                10.0,
                20.0
            ]
        ]
}

with this structure, we know that every varname.DATA[index][0] is refers to USER

tsurahman