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

Asked

Viewed 53 times

0

Follow 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 receive 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 cannot access the data in aluno.mensalidades.2018.jan, whereas in dAula.Quarta I get it, follow the mistake:

Uncaught TypeError: Cannot read property 'jan' of undefined

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

1 answer

2


Buddy, I tested your code and it worked right! I just found a little strange the way you declared the object, but with me it worked smoothly, I only 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']   );

  • I think my problem lies in the way I’m pulling the object from the bank. I’ll edit the question

  • 1

    I believe that no matter how you do it, using the dot syntax will not work. You need to select using [] when the property name is a number or starts with any value other than _ $ or a letter. Do obj.mensalidades[2018].jan works perfectly, what your difficulty?

  • Using aluno.mensalidades[2018].jan, inside the map returns the error, Uncaught TypeError: Cannot read property 'jan' of undefined , I have tried putting quotes too, as for example aluno.mensalidades['2018'].jan

  • Sorry, the answer is correct, what happened was that some students did not have this value in the bank ( because they did not pay) I ended up not filtering them and the error was generated by not entering the value inside the object

Browser other questions tagged

You are not signed in. Login or sign up in order to post.