Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

93
Vistas
Updating object in nested array

I can't figure out how to make a javascript function to update the "checked" value inside the object in an array like this

array:[
  { id:1,
    name:"foo"
    options: [
      {id:"one", checked: false},
      {id:"two", checked: true}
    ]
  },
  { id:2,
    name:"faa"
    options: [
      {id:"one", checked: false},
      {id:"two", checked: true}
    ]
  }
 ]

I know how to do it with array of objects but am stuck with this. Any suggestions into what I need to do? Thanks

Update typo in array format

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

If I understand your requirement correctly. you want to update the checked property via checkbox in HTML template.

Demo :

new Vue({
  el: '#app',
  data: {
    array: [{
        id:1,
        name:"foo",
        options: [
        {id:"one", checked: false},
        {id:"two", checked: true}]
    }, {
        id:2,
        name:"faa",
        options: [
        {id:"one", checked: false},
        {id:"two", checked: true}]
    }]
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <div v-for="data in array" :key="data.id">
    <b>{{ data.name }}</b>
    <div v-for="(checkboxData, index) in data.options" :key="index">
      {{ checkboxData.id }} : <input type="checkbox" v-model="checkboxData.checked"/>
    </div>
  </div><br>
  <b>Updated Value : </b>
  <p>{{ array }}</p>
</div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

You could use map() to create a new array of objects with the updated values.

const data = [{
    id: 1,
    name: "foo",
    options: [
      { id: "one", checked: false },
      { id: "two", checked: true }
    ]
  },
  {
    id: 2,
    name: "faa",
    options: [
      { id: "one", checked: false },
      { id: "two", checked: true }
    ]
  }
];

const result = data.map((d) => {
  return {
    ...d,
    options: d.options.map((o) => {
      return {
        ...o,
        checked: !o.checked // for example invert the checked value
      }
    })
  }
});

console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda