Home > Net >  JavaScript object array new values
JavaScript object array new values

Time:06-28

This is my first question post :) I have the following array object:

<script>
var tratamentos = {
      "Sr": ["Casado", "Solteiro"],
      "Sra": ["Casada", "Solteira"],}
</script>

And I want to add, for example in "Sr" only the values: "Desquitado" and "Enrolado". Could someone give me some help? I'm racking my brains over this simple thing.

Thanks :)

CodePudding user response:

You can acces items from the nested array using dots (".") and then use .push operator to add items to the array.

var tratamentos = {
      "Sr": ["Casado", "Solteiro"],
      "Sra": ["Casada", "Solteira"],}
tratamentos.Sr.push("Desquitado", "Enrolado");
console.log(tratamentos);

  • Related