Skip to content

Question: How to iterate over maps and sets with v-for #6644

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
yoyo837 opened this issue Sep 19, 2017 · 12 comments
Closed

Question: How to iterate over maps and sets with v-for #6644

yoyo837 opened this issue Sep 19, 2017 · 12 comments

Comments

@yoyo837
Copy link

yoyo837 commented Sep 19, 2017

What problem does this feature solve?

How to iterate over Map and Set with v-for ? I want to use the key/index field or both.(ES6 Map/Set)

<mt-index-list>
  <mt-index-section v-for="(list, key) in serverData.indexMap" :index="key" :key="key">
	<mt-cell title="Aaron"></mt-cell>
  </mt-index-section>
</mt-index-list>

What does the proposed API look like?

N/A

@sqal
Copy link
Contributor

sqal commented Sep 19, 2017

Maps and sets are not currently supported in v-for, heard it will be possible in Vue 3.0

@yyx990803
Copy link
Member

It's currently not supported in core (there's no reactivity detection for these data structures). But you can use (list, key) in Array.from(map) + $forceUpdate() to simulate it.

@yoyo837
Copy link
Author

yoyo837 commented Sep 19, 2017

@yyx990803 @sqal
OK, I got it. Thanks for your reply.

@CreatorMr
Copy link

Thanks

@Dwood15
Copy link

Dwood15 commented Oct 31, 2018

For those coming from google, this is not supported, nor is it planned to be.

Please see this issue: #2410

@Vineet-Sharma29
Copy link

if not v-for then is there any other way to iterate over map in vuejs?

@ambyjkl
Copy link

ambyjkl commented Feb 28, 2021

Sometimes you don't want reactivity, you just want to iterate over static data keyed by types other than string. It would be nice if v-for or a similar feature supported anything that implements Symbol.iterator

@ddosanjh
Copy link

ddosanjh commented May 4, 2021

This is still a top-ranked result for google searches on iterating a Map with v-for, so for those landing here as I did, in Vue 3 (vue-next as of this writing), reactivity on Map (and Set) is supported. And you can conveniently write a v-for loop using array destructuring syntax:

<div v-for="[fooKey, barValue] in fooBarMap" >
  {{fooKey}} is {{barValue}}
...

@Archieeeeee
Copy link

This is still a top-ranked result for google searches on iterating a Map with v-for, so for those landing here as I did, in Vue 3 (vue-next as of this writing), reactivity on Map (and Set) is supported. And you can conveniently write a v-for loop using array destructuring syntax:

<div v-for="[fooKey, barValue] in fooBarMap" >
  {{fooKey}} is {{barValue}}
...

Thank you, you saved my day.

@RobertoMaurizzi
Copy link

Using a Map should solve the ugly problem of not having a standard-defined enumeration order for the keys of an Object

@suterma
Copy link

suterma commented Jan 14, 2022

I needed to also have a key, like (in Vue3)

    <div v-for="[fooKey, barValue] in fooBarMap" :key="fooKey">
        {{ fooKey }} is {{ barValue }}
    </div>

@matthew-dean
Copy link

Apparently you can also use of, like:

<div v-for="[fooKey, barValue] of fooBarMap" >

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests