I have a json in the following structure:
$scope.hi=[{
"a":1,
"b":true,
"c":"great"
}];
I want to extract only the keys and make an array like
$scope.bye=["a","b","c"];
Though seems to be a very basic question but would be very helpful for me.
This is what you need
Object.keys($scope.hi[0]);
This only works in IE9+ if you target IE.
An alternative might be to do fetch them with a loop
var obj = $scope.hi[0],
array = [];
for (key in obj) {
if (obj.hasOwnProperty(key)) {
array.push(key);
}
}
Also note that the order of the keys may not be respected depending on the browser implementation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With