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

Vue Checkbox get an array value but want Array of JSON Objects

enter image description here

<checkbox-group @change="checkboxChange">
                <label class="uni-list-cell uni-list-cell-pd" v-for="item in items" :key="item.value">
                    <view>
                        <checkbox :value="item.value" :checked="item.checked" />
                    </view>
                    <view>{{item.name}}</view>
                </label>
            </checkbox-group>

The data variable from the checkbox is

items: [
        {
           value: 'USA',
           name: 'American'
        },

         {
           value: 'JPN',
           name: 'Japan'
         },
         {
           value: 'ENG',
           name: 'England'
         },
         {
           value: 'FRA',
           name: 'France'
         }
 ]

When check the box for selecting different countries, will get this value.
["JPN", "ENG"]

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

checkboxChange: function(e) {
                var items = this.items,
                      values = e.detail.value; 
            
                console.log(values) #get value ["JPN", "ENG"]
}

But the desired result is Array of JSON Objects like below

new_items: [ 
       {
         value: 'JPN',
         name: 'Japan'
       },
       {
         value: 'ENG',
         name: 'English'
       }
]

How can I use this value ["JPN", "ENG"] to map with checkbox items to get new_items?

>Solution :

Try to bind the whole object to the value prop :

<checkbox :value="item" :checked="item.checked" /> 
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