D
D
DemonFromIrk2019-04-03 19:53:17
Vue.js
DemonFromIrk, 2019-04-03 19:53:17

How to write part of an array in vue?

I make an API request and get the following response:$.get('/api/v2/staff/role/' + this.roleData.id)

spoiler
{
    "id": "1",
    "status": "1",
    "name": "Администратор",
    "rights": {
        "orders.view": "1",
        "orders.create": "1",
        "orders.changeClosed": "0",
        "main.managerList": "1",
        "main.workerList": "1",
        "finance.reports": "1",
        "compendiums.counteragents": "1",
        "compendiums.works": "1"
    },
    "error": {
        "error_code": 0
    }
}

How do I write an array of response rights with a value of 1 into the vue checkRights array (I use it for checkboxes) ?
data:{
  test: null,
  modalAdd: false,
 roleData:{
     id:null,
     name: null,
     checkRights: []
                }
 }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Yarkov, 2019-04-03
@DemonFromIrk

const response = {
    "id": "1",
    "status": "1",
    "name": "Администратор",
    "rights": {
        "orders.view": "1",
        "orders.create": "1",
        "orders.changeClosed": "0",
        "main.managerList": "1",
        "main.workerList": "1",
        "finance.reports": "1",
        "compendiums.counteragents": "1",
        "compendiums.works": "1"
    },
    "error": {
        "error_code": 0
    }
};

const roleData = {
  checkRights: []
};

const rights = Object.keys(response.rights).filter((key) => +response.rights[key] === 1);

roleData.checkRights = roleData.checkRights.concat(rights);

console.log(roleData);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question