0

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 :)

3
  • 2
    tratamentos.Sr.push('Desquitado') Commented Jun 27, 2022 at 16:27
  • how do you wanna update the value of "Sr", like do you want to add new values? Commented Jun 27, 2022 at 16:28
  • 3
    tratamentos["Sr"].push("Desquitado", "Enrolado"), take a look at accessing objects and Array#push Commented Jun 27, 2022 at 16:29

1 Answer 1

2

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);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.