I have a data set which has a number of devices with similar properties (output). (for example 2 given below). what I do not understand is how do I take each device with the new name and assign it to for example a new div. There can be many devices in dataset. How do I tell my code to whenever there is a new device take out bytes_read and bytes_written and assign them to a new div for example assign
"bdev0": {"Bytes_Read": 0, "Bytes_Written": 0} to div_one
"bdev1": {"Bytes_Read": 10, "Bytes_Written": 20 } to div_two
Note that I cannot use something like data.devices[0] because there are many devices and their names keep changing, e.g bdev0, bdev1, bdev2, bde3.. here is the sample of dataset given:
var data = {
"devices": [
{
"Name": "bdev0",
"output": {
"IO_Operations": 0,
"Bytes_Read": 0,
"Bytes_Written": 0
}
},
{
"Name": "bdev1",
"output": {
"IO_Operations": 10,
"Bytes_Read": 20,
"Bytes_Written": 30
}
}
]
}
This is how far I could come but it creates two different strings but how do I assign these separately to two different items. it sounds really stupid but I am really stuck here if for example I want to assign these strings to var a and var b how should I do it
function myData() {
for (var i in data.devices){
var obj = new Object();
obj.Bytes_read = data.devices[i].output.Bytes_Read;
obj.Bytes_written = data.devices[i].output.Bytes_Written;
var jsonString= JSON.stringify(obj);
console.log(jsonString)
}
}
myData(data)
Result
{"Bytes_read":0,"Bytes_written":0}
{"Bytes_read":20,"Bytes_written":30}
It gives me the data I want but I cannot figure out to assign these sets to var a and var b.
bdev0andbdev4be relevant, but anything else to be irrelevant?