Search code examples
javascriptvue.jsreturn

Vuejs returning with { ...}


Iam currently working with a project where project is almost partially completed. So I just saw a some weired code whihc I havent seen before and tried to find it via google and couldnt find anythig.

This is the code

methods:  {

getMap() {
   let A = this.$store.getters['MAP']
   let B = this.$store.getters['MAP2']

   return { ...A, ...B}
 }

}

What I really wants to know is

return { ...A, ...B}

what does it means ? any reference or any clarification please.


Solution

  • It's javascript spread syntax. What it does is merge two objects by keys, similar to Object.assign. For instance, in the following example, keys from A and B are merged into the final object with values from the latter object taking precedence.

    const A = { a: 3, b: 4 }
    const B = { c: 5, d: 7, a: 4 }
    
    console.log({ ...A, ...B })

    In your case, getMap return a combined object with key value pairs from Map and Map2 merged.