0

I'm working on an vue-application where I have a component for driving licenses.

I have the following:

data() {
   return {
     custom_licenses: [],
     basic_licenses: []
   }
}

within my methods, I have this:

regular_licenses() {
  this.$store.dispatch("license/read").then(response => {
    response.licenses.map((license, key) => {
      // PUSH LICENSES WITH TYPE 'BASIC' TO this.basic_licenses
      // PUSH LICENSES WITH TYPE 'CUSTOM' TO this.custom_licenses
    });
  });
},

and in my created() i have this:

created() {
   this.regular_licenses()
}

The response from my dispatch, returns this:

licenses: 
 [
   {
     id: 1, 
     type: 'basic', 
     name: 'AMa'
   },
   {
     id: 2, 
     type: 'basic', 
     name: 'A2'
   }, 
   {
     id: 3, 
     type: 'basic', 
     name: 'C'
   },
   {
     id: 4, 
     type: 'custom', 
     name: 'C1'
   }, 
   {
     id: 5, 
     type: 'custom', 
     name: 'D'
   },

   and so on...
 ]

Now I want to loop through the array and separate or push them into custom_licenses and basic_licenses based on the type-attribute - how can I achieve that?

2 Answers 2

1

Try this

regular_licenses() {
  this.$store.dispatch("license/read").then(response => {
    response.licenses.map((license, key) => {
      switch (license.type)
        case 'basic':
          this.basic_licenses.push({ ...license });
          break;
        case 'custom':
          this.custom_licenses.push({ ...license });
          break;
    });
  });
},
Sign up to request clarification or add additional context in comments.

Comments

0

Update your Code Block:

 response.licenses.map((license, key) => {
   // PUSH LICENSES WITH TYPE 'BASIC' TO this.basic_licenses
    if(license['type'] == 'basic') {
        //deep clone
        let tmpLicense = JSON.parse(JSON.stringify(license));
        basic_licenses.push(tmpLicense);
    } else if(license['type'] == 'custom') {
    // PUSH LICENSES WITH TYPE 'CUSTOM' TO this.custom_licenses        
        //deep clone
        let tmpLicense = JSON.parse(JSON.stringify(license));
        custom_licenses.push(tmpLicense);
    }  
  });

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.