2

I've been banging my head against a wall with this one for a few hours and for some reason, I don't seem to be able to get to where I need. I'm sure it's simple and I can quite happily do this with python, just not with Javascript :(

I'm trying to loop through some Json from another page using Ajax. I've knocked together a quick dummy of how the JSON returns.

I seem to be able to get the key but somehow I'm unable to get the actual values within the list of dictionaries:

<script>
var data = {
    "test": [
                {
                    "cpu_count": 4,
                    "memory_size_MiB": 6144,
                    "power_state": "POWERED_ON",
                    "vm": "vm-1173"
                },
                {
                    "cpu_count": 8,
                    "memory_size_MiB": 2048,
                    "power_state": "POWERED_ON",
                    "vm": "vm-1173"
                }
    ]
}


for(var key in data) {
  var value = data[key];
  console.log(key)

}
</script>

Is anyone able to point me in the right direction here?

To give context, this is being pulled from Redis within Celery - so the end goal is to make a table within Flask of the dictionary values pulled.

Thanks in advance

  • There's nothing really to iterate over here - you're pretty much saying "for each element in the JSON object, print out the key", and there's only one key in the root JSON document. – James Whiteley Apr 29 '19 at 15:12
  • There are many such questions on SO in case that one's not right. It's all been covered thoroughly. – isherwood Apr 29 '19 at 15:12
  • You're also creating a variable `value` and never using it - is this intentional? – James Whiteley Apr 29 '19 at 15:12
  • You can use the `jQuery` `$.each()` function to loop over your data. – dwpu Apr 29 '19 at 15:13
  • Or just the native forEach, since data.test is an array... – Taplar Apr 29 '19 at 15:13
  • I think you'll have better luck searching for how to iterate through "JSON objects" or "javascript objects" rather than "dictionaries" when playing around with JS... "Dictionary" isn't a common term in javascript; it's a more python-y term. – James Whiteley Apr 29 '19 at 15:16
  • For example, you can use a modified version of the recursive part of [this answer](https://stackoverflow.com/a/2549333/8230810) if you want to just print every key and value which has a non-object-or-array value, or something like [this answer](https://stackoverflow.com/a/684692/8230810) if you don't have a deep structure or have an idea of what the JSON will look like (etc etc etc) – James Whiteley Apr 29 '19 at 15:24

1 Answers1

0

If you only want to loop and get information you can use forEach or $.each of jquery like this.

data.test.forEach((element) => {
    console.log(element.cpu_count);
});

$.each(data.test, function(index, item){
   console.log(item.vm);
});

You can use map function to loop your array object and transform to another object.

data.test.map(c=> { 
  console.log(c.cpu_count)
  console.log(c.memory_size_MiB)
  console.log(c.power_state)
  console.log(c.vm)
});

var data = {
    "test": [
                {
                    "cpu_count": 4,
                    "memory_size_MiB": 6144,
                    "power_state": "POWERED_ON",
                    "vm": "vm-1173"
                },
                {
                    "cpu_count": 8,
                    "memory_size_MiB": 2048,
                    "power_state": "POWERED_ON",
                    "vm": "vm-1173"
                }
    ]
}
data.test.forEach((element) => {
    console.log(element.cpu_count);
});

$.each(data.test, function(index, item){
   console.log(item.vm);
});

data.test.map(c=> { 
console.log(c.cpu_count)
console.log(c.memory_size_MiB)
console.log(c.power_state)
console.log(c.vm)
});

/*
for(var key in data) {
  var value = data[key];
  console.log(key)

}
*/
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62