How to access an object within an object, and its property name is a number?

0

Here is an example of the object in question:

 obj : {

  "bairro" : "Centro",

  "dAula" : {
    "Quarta" : true,
    "Segunda" : true
  },

  "dVencimento" : "10",

  "dataCadastro" : "10/10/2017",

  "dataInicioContrato" : "27/11/2018",

  "mensalidades" : {

    "2018" : {

      "fev" : true,
      "jan" : true,
      "mar" : true
    }
  },
  "numero" : "111 apto 201",
}

I get a list of students by this.props.list and include them in a table as follows:

class TabelaMensalidade extends Component {
  render() {
   return(
       <tbody>
        {
         this.props.lista.map((aluno)=>{
          return (
            <tr key={aluno.key}>
            <td>{aluno.bairro}</td>
            <td>{aluno.dVencimento}</td>
            <td>{aluno.dataCadastro}</td>
            <td>{aluno.dataInicioContrato}</td>
            <td>{aluno.dAula.Quarta}</td>
            <td mes='jan' data-value={aluno.mensalidades.2018.jan}>{aluno.mensalidades.2018.jan}
           </tr>
          )
        })
       }
      </tbody>
     )
    }
  } 

However, I can not access the data in aluno.mensalidades.2018.jan , and in dAula.Quarta I can, follow the error:

Uncaught TypeError: Cannot read property 'jan' of undefined

I have tried to use aluno.mensalidades[2018].jan and aluno.mensalidades['2018'].jan and the error remains the same

    
asked by anonymous 28.11.2018 / 05:11

1 answer

2

Friend, I tested your code and it worked right! I just found it a little strange the way you stated the object, but with me it worked quietly, I just used the code as follows:

//Declaro uma variável chamada 'obj' como objeto
var obj = {

  "bairro" : "Centro",

  "dAula" : {
    "Quarta" : true,
    "Segunda" : true
  },

  "dVencimento" : "10",

  "dataCadastro" : "10/10/2017",

  "dataInicioContrato" : "27/11/2018",

  "mensalidades" : {

    "2018" : {

      "fev" : true,
      "jan" : true,
      "mar" : true
    }
  },
  "numero" : "111 apto 201",
}


//mando escrever no documento o resultado das mensalidades de jan de 2018
document.write(   obj['mensalidades']['2018']['jan']   );
    
28.11.2018 / 05:26