Reputation: 1475
I have a data set
$scope.mydata = [{
"Block_Devices": {
"bdev0": {
"Backend_Device_Path": "/dev/ram1",
"Capacity": "16777216",
"Bytes_Written": 1577,
"timestamp": "4365093970",
"IO_Operations": 17757,
"Guest_Device_Name": "vdb",
"Bytes_Read": 17793,
"Guest_IP_Address": "192.168.26.88"
},
"bdev1": {
"Backend_Device_Path": "/dev/ram2",
"Capacity": "16777216",
"Bytes_Written": 1975,
"timestamp": "9365093970",
"IO_Operations": 21380,
"Guest_Device_Name": "vdb",
"Bytes_Read": 20424,
"Guest_IP_Address": "192.168.26.100"
}
},
"Number of Devices": 2
}]
and I would like to create a array from this json such as
devices = ['bdev0', 'bdev1']
when I try
$scope.mydata.Block_Devices
it gives me the whole json object but I just want the names of the objects i.e bdev0 and bdev1 how can I get that?
Upvotes: 0
Views: 992
Reputation: 509
Try this:
var devices = [];
for (var key in $scope.mydata[0].Block_Devices) {
devices.push(key)
}
Upvotes: 1
Reputation: 45121
Just in case ES5 solution
devices = Object.keys($scope.mydata[0].Block_Devices)
Upvotes: 1
Reputation: 948
You have to loop through the object properties to archive this:
var devices = [];
var data = $scope.mydata[0].Block_Devices;
for (var name in data) {
if (data.hasOwnProperty(name)) {
devices.push(name);
}
}
The hasOwnProperty call is important to skip the properties which are from prototype, if you are sure that there are none, you can skip this.
Upvotes: 0