0

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?

1
  • devices = Object.keys($scope.mydata[0].Block_Devices) Commented May 11, 2016 at 11:13

3 Answers 3

1

Try this:

var devices = [];

for (var key in $scope.mydata[0].Block_Devices) {
    devices.push(key) 
}
Sign up to request clarification or add additional context in comments.

Comments

1

Just in case ES5 solution

devices = Object.keys($scope.mydata[0].Block_Devices)

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.