Since i already started looking into it:
var data = [{
"id": "1",
"msg": "hi",
"tid": "2013-05-05 23:35",
"fromWho": "hello1@email.se"
}, {
"id": "2",
"msg": "there",
"tid": "2013-05-05 23:45",
"fromWho": "hello2@email.se"
}]
And this function
var iterateData =function(data){ for (var key in data) {
if (data.hasOwnProperty(key)) {
console.log(data[key].id);
}
}};
You can call it like this
iterateData(data); // write 1 and 2 to the console
Update after Erics comment
As eric pointed out a for in
loop for an array can have unexpected results. The referenced question has a lengthy discussion about pros and cons.
Test with for(var i ...
But it seems that the follwing is quite save:
for(var i = 0; i < array.length; i += 1)
Although a test in chrome had the following result
var ar = [];
ar[0] = "a";
ar[1] = "b";
ar[4] = "c";
function forInArray(ar){
for(var i = 0; i < ar.length; i += 1)
console.log(ar[i]);
}
// calling the function
// returns a,b, undefined, undefined, c, undefined
forInArray(ar);
Test with .forEach()
At least in chrome 30 this works as expected
var logAr = function(element, index, array) {
console.log("a[" + index + "] = " + element);
}
ar.forEach(logAr); // returns a[0] = a, a[1] = b, a[4] = c
Links