Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading