I will illustrate what I want to do with a trivial example. Let's say I have a variable rows with the content:
[ { id: 1,
admin_id: 2,
start_date: Mon Nov 18 2013 07:19:46 GMT-0800 (PST),
status: 1,
username: 'Marko Markovic' },
{ id: 7,
admin_id: 3,
start_date: Mon Nov 18 2013 07:20:32 GMT-0800 (PST),
status: 0,
username: 'Dzoni Noksvil' } ]
Now, what I want to create is the following:
var gl = {
1: { admin: "Marko Markovic" , status: "1" },
7: { admin: "Dzoni Noksvil" , status: "0" }
};
So, I need to loop through rows and, for each row, add a row to the gl object according to the rule which i think is clear from the example above.
So I tried this.
var gl;
rows.forEach(function(row) {
gl[row.id] = { admin: row.username, status: row.status };
});
The above code throws an error saying Cannot set property '1' of undefined. I also tried the following, knowing it won't work but curious as to what error it will throw:
var gl;
rows.forEach(function(row) {
gl.row.id = { admin: row.username, status: row.status };
});
And the error was Cannot read property 'row' of undefined.
Can someone tell me how to do this correctly and, if possible, the meaning of the errors because I don't know what is supposed to be undefined here.
Oh, and please, plain Javascript only, even if jQuery has an easier/better solution.