Anto Anto - 1 month ago 5
Javascript Question

Reading values from a flatten JSON

I have created an array (named boxesList) of flatten Json objects, where each of these objects looks like this:

Object{
iips:"ALP872"
box.number:142
box.color:"blue"
box.size:11
}


When I try to extract in my application the data stored in these objects:

for(let boxElement of boxesList){
console.log(boxElement.iips);
console.log(boxElement.box.number);
}


I do not have issue getting the iips value, but I do get 'Uncaught TypeError: Cannot read property 'number' of undefined' when trying to get box.number. Does anyone know how can this be achieved?

Answer

You property key is box.number

boxElement.box.number calls the number property inside the box property

var boxesList = [{
   "iips":"ALP872",
   "box.number":142,
   "box.color":"blue",
   "box.size":11
}]

for(let boxElement of boxesList){
    console.log(boxElement.iips);
    console.log(boxElement["box.number"]);
}