I have a JSON array and I want to extract the key names associated with each object.
Despite my initial search efforts turning up no effective solutions, I managed to create my own approach:
var data = [{ Id: 25 }, {Year: 18} ];
$.each(data, function (i, element) {
var stringfied = JSON.stringify(element);
var parts = stringfied.split('":'); // separate key and value
var key = parts[0].split('{"')[1]; // extract key
console.info(key);
console.info(element[key]);
});
I also experimented with other methods such as:
for (var key in data) {
console.info(key);
info(data[key]);
}
However, these approaches failed to display the key names accurately.
Is there a more efficient solution available? (Note that Object.keys may not work on older browser versions)