Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

236
Views
v-for using Map data structure and v-model

I know it is possible to use Map with v-for, but say, is it possible to use the values [key, value] as v-model?

Ex:

<input v-for="[key, value] in map" :key="key" v-model="key"></input>
...
...
...
data(){
    return {
        map: new Map([['cool thing 1', 'cool thing 2'], ...])
    }
}

Does Vue3 support this natively?

If not, what could be done?

To be more precise i want to use the key and value thenselves as v-model in inputs

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I think Vue has only two limitations on what can be used with v-model

  1. the expression must be valid "left side" for assignment (because v-model="xxx" is same as :value="xxx" @input="xxx = $event")

  2. Both pairs - (key, value) when iterating objects and (value, index) when iterating arrays are local temp variables of the loop so it does not make any sense to use them in the v-model. If you try that you will get a warning: You are binding v-model directly to a v-for iteration alias. This will not be able to modify the v-for source array because writing to the alias is like modifying a function local variable.

So you need to make a step back and use the original object/array with v-model

Arrays: v-model="arr[index]"

Objects/Maps: v-model="map[key]"

Update

My idea its basically the user can input both key and value for a map

That is a bad idea for a few reasons. First, as said before key variable is a local temp variable in the loop so using it in v-model is not possible. Second (again - as said before) whatever you put inside v-model must be "assignable". Map object is not assignable - you must use a set method to set a value of particular key. So v-model is not an option here (you must use events instead). On top of that, Map does not allow duplicate keys so letting users freely edit the keys is recipe for a very weird UX....

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!