This is what I get as a jQuery response from Wordpress database after json_encode:
[{ "id" : "33", "first_name" : "Oleg", "last_name" : "Smith" }]
Because it is multi-dimensional in nature, but there is just one row, hence you have square brackets on both ends.
Therefore ALL methods I found on SO when trying to parse and then convert to a JS array fail, e.g.
for(var x in parsed){
arr.push(parsed[x]);
}
OR
var arr = $.map(obj, function(el) { return el; });
OR
var arr = Object.keys(obj).map(function(k) { return obj[k] });
OR
for(var i in JsonObj) {
if(JsonObj.hasOwnProperty(i) && !isNaN(+i)) {
array[+i] = JsonObj[i];
}
}
I can manually remove square brackets and proceed OR I can push manually each item:
aaData.push(searchObj[0].first_name);
But I don't like either of the solutions I have. Do I have other options?
PS. I need a JS array so that I can loop through with [i].
{ "id" : "33", "first_name" : "Oleg", "last_name" : "Smith" }. If so, can't you access the object asresponse[0]and then use afor inloop?