N
N
nastya_zholudeva2018-09-13 16:29:33
JavaScript
nastya_zholudeva, 2018-09-13 16:29:33

How to form such an array of data from the selected checkboxes?

example here

"filters": [
            {
                "type": "checkbox",
                "title": "Пол",
                "filterData": {
                    "variants": [
                        {
                            "key": 14,
                            "value": "Мужской"
                        },
                        {
                            "key": 15,
                            "value": "Женский"
                        }
                    ]
                },
                "filterId": 1
            },
{
                "type": "checkbox",
                "title": "num",
                "filterData": {
                    "variants": [
                        {
                            "key": 2,
                            "value": "345"
                        },
                        {
                            "key": 1,
                            "value": "123"
                        }
                    ]
                },
                "filterId": 2
            }
]

The user selects several of these checkboxes and presses the "Select" button. How to form such an array of data?
[
{ "filterId": 1, values: [14,15]},
{ "filterId": 2, values: [1]},
]

PS filters comes via props from vuex

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2018-09-13
@nastya_zholudeva

The user selects several of these checkboxes and presses the "Select" button.

Why a button? You can also make a computed property, the selected elements will be available at any time (of course, if you do not forget to bind the data to the checkboxes via v-model), something like this:
selected() {
  return this.filters.map(n => ({
    filterId: n.filterId,
    values: n.filterData.variants.filter(m => m.checked).map(m => m.key)
  }));
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question