-3

i need to access values of "models" and "unique model" for each sensors, the json you see is a simulation of what my database will output me.

        var availableSensors =
        {
            "sensors" :
            {
                
                "thermometer":
                {
                    "models" : ["thermo333","withingsthermo"],
                    "default_model" : "withingsthermo",
                    "countable" : false
                },

                "blood_pressure":
                {

                    "models" : ["blood3000","gloubibloodP"],
                    "default_model" : "bloodyP",
                    "countable" : false
                },

                "oxymeter":
                {
                    "models" : ["oxy3000","bipbipTot"],
                    "default_model" : "oxy3000",
                    "countable" : false
                },

                "scale":
                {
                    "models" : ["withingsscale","boulgascale","magicweight2000"],
                    "default_model" : "withings_scale", 
                    "countable" : false 
                },

                "smartphone": 
                {
                    "models" : ["S10","oppoPascher"],
                    "default_model": "withings_scale",
                     "countable": true
                },
            
            }

        };
    return availableSensors;

    } 

Ive managed to gather all my sensors (thermometer, blood pressure etc...) by doing a Object.keys(availableSensors.sensors)

what i need, is to be able to Object.keys(availableSensors.sensors.[sensor_name].models);

I dont want to have to write : Object.keys(availableSensors.sensors.thermometer.models);

any solution please ? [AUTO RESOLVED]Access value of a property, which is inside a proprety, which is also inside a property, in a JSON EDIT

i managed to acces them sneaky properties by doing :

for(var sensorType in availableSensors["sensors"])

If it can help so.... merlich pour l'aide....

Monsluxe
  • 1
  • 2
  • Personally I'd just loop through the object using a `for .. in` loop instead of using `Object.keys`. This is just a 3 level nested for loop – slebetman May 06 '22 at 12:43
  • ye boss i agree with you but my problem is not here, i dont wanna have to write for (models in availableSensors.sensors.thermometer), i need a var that take care of each sensors in the json just has if i was doing a : for (models in availableSensors.sensors[i].models) { must output the models } – Monsluxe May 06 '22 at 13:09
  • No. I think you don't understand. You write `for (s in avaliableSensors.sensors) { let sensor = availableSensors.sensors[s]; let models = sensor.models; console.log(models)}` – slebetman May 07 '22 at 12:04
  • i tried it but models appear undefined, if i do availableSensors.hasOwnProperty('models'); it return false, same for sensors.... it seems i cannot access properties after each sensors... – Monsluxe May 09 '22 at 13:31
  • `availableSensors` contains only the property `sensors`. It does not contain `models`. You have not tried my for loop. If you have you'd realize that I was not doing `availableSensors.hasOwnProperty('models')` but `availableSensors.sensors[sensor_name]hasOwnProperty('models')` – slebetman May 10 '22 at 01:03

0 Answers0