Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angularjs: Create an array of keys from json object

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.

like image 986
ritz078 Avatar asked Mar 26 '26 05:03

ritz078


1 Answers

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.

like image 110
axelduch Avatar answered Mar 28 '26 19:03

axelduch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!