0

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.

4
  • Are you going to receive those datas synchronously or asynchronously? Also, is the amount of data previously known or unknown? Commented Oct 19, 2015 at 14:50
  • asynchronously and the amount is unknown Commented Oct 19, 2015 at 14:53
  • But do you need every data to be processed, or just specific parts of it? i.e., can bdev0 and bdev4 be relevant, but anything else to be irrelevant? Commented Oct 19, 2015 at 14:56
  • there is nothing else in the data, there are only names of devices e.g bdev0 bdev5 and their outputs so I think I need to process all data Commented Oct 19, 2015 at 15:03

2 Answers 2

2

If you have the name of the device you can use it as key for the access of the data.

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
            }
        }]
    },
    selectedData = {};

data.devices.forEach(function (a) {
    selectedData[a.Name] = {
        Bytes_Read: a.output.Bytes_Read,
        Bytes_Written: a.output.Bytes_Written
    };
});
document.write('<pre>'+JSON.stringify(selectedData, 0, 4)+'</pre>');

Update: Maybe this is what you want. With the name of the device, the function getDeviceInfo returns the information.

function getDeviceInfo(deviceName) {
    var obj = {};
    data.devices.some(function (a) {
        if (a.Name === deviceName) {
            obj[deviceName] = {
                Bytes_Read: a.output.Bytes_Read,
                Bytes_Written: a.output.Bytes_Written
            };
            return true;
        }
    });
    return obj;
}

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
            }
        }]
    },
    div_one = getDeviceInfo('bdev0'),
    div_two = getDeviceInfo('bdev1');

document.write('<pre>' + JSON.stringify(div_one, 0, 4) + '</pre>');
document.write('<pre>' + JSON.stringify(div_two, 0, 4) + '</pre>');

Sign up to request clarification or add additional context in comments.

3 Comments

yes but 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
@Imo, from where do you know, what name is important? where do you get the name (like bdev0 or bdev1) from?
these are coming from a vm and the data is uniform all it has are json objects like the one in the dataset, every property with Name is important
0

I'm not quite sure what you're trying to achieve. The code below will take the output property of each element in array and set a to its value when Name is bdev0, and b to it otherwise. I hope this is what you needed:

var a, b;
if (data[0]['Name'] === 'bdev0') {
    a = JSON.stringify(data[0]['output']); // bdev0
    b = JSON.stringify(data[1]['output']); // bdev1
} else {
    a = JSON.stringify(data[1]['output']); // bdev1
    b = JSON.stringify(data[0]['output']); // bdev0
}

3 Comments

Thank you for taking time to answer my question but what I am after is that each time there is a new 'Name' element, eg bdev1, bdev4 I can takeout its bytes_written and bytes_read properties and assign them to another variable. the actual dataset is exactly same as the sample data set but with many other devices
If you only need Bytes_Read and Bytes_Written, Nina's code should work for you.
unfortunately her answer does not answer my question perhaps I am not able to explain myself correctly. I just need a way to assign "bdev0": {"Bytes_Read": 0, "Bytes_Written": 0} to div_one and "bdev1": {"Bytes_Read": 0, "Bytes_Written": 0 } to div_two

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.