0

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?

Anto
  • 4,265
  • 14
  • 63
  • 113

3 Answers3

3

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"]);
}
Weedoze
  • 13,683
  • 1
  • 33
  • 63
0

In this case you have to use the boxElement['box.number'] syntax.

The boxElement is indexed on strings like box.number, rather than on two layers.

Hugh Rawlinson
  • 979
  • 9
  • 29
0

You cant have an object property name with a dot in it. What you're looking to do above can be achieved if 'box' is a property of the main object with 'number', 'color' and 'size' being it's properties.

Something like this:

var mainObj = {
   iips:"ALP872",
   box: {
       number:142,
       color:"blue",
       size:11
   }
}