DEV Community

Hands-on Vue.js for Beginners (Part 4)

Marina Mosti on February 14, 2019

Welcome back! 🤩 Last time we took at conditional rendering with v-if and v-show. This time we will learn how to loop through arrays and objects an...
Collapse
 
namstel profile image
Namstel • Edited

Thanks for the tutorial, Marina. Here's my solution:

HTML:

<span v-for="star in game.rating" @click="increaseRating(game)"></span>

JS bit:

methods: {
        increaseRating(game){
        game.rating++;
    }
},

Now on to the next! :D

Collapse
 
marinamosti profile image
Marina Mosti

Yay, thanks for reading!

Collapse
 
lukmanarifs profile image
Lukman Arif Sanjani

surprised with the other answer.. short and simple...
why i make it complicated... LOL

addRating (i) {
      this.$set(this.games[i], 'rating', this.games[i].rating + 1)
    }

btw thanks Marina, nice tutorial

Collapse
 
marinamosti profile image
Marina Mosti

All ways lead to Rome -^ Thanks for reading! <3

Collapse
 
akwetey profile image
Jonathan Akwetey

great one again. i'm accepting the challenge. i will post my answer soon

Collapse
 
marinamosti profile image
Marina Mosti

w00t!

Collapse
 
akwetey profile image
Jonathan Akwetey

Alt text of image

Thread Thread
 
akwetey profile image
Jonathan Akwetey

hey marina please check it and let me know of any improvements or corrections. Thanks

Thread Thread
 
marinamosti profile image
Marina Mosti

Hey Jonathan, awesome job! I wasn't expecting you to catch on the (game, index) since i didn't cover it on the tutorial - but job well done finding that out on your own! :)

Thread Thread
 
akwetey profile image
Jonathan Akwetey

thanks. i will be looking forward to more challanges that pushes me to learn more. I hope you will cover the (game, index) in a different tutorial.

Thread Thread
 
agredalex profile image
Alex Ágreda

@click="game.rating += 1", adding this on the span also works

Collapse
 
emmanuelnkechi5 profile image
Enkay💗

HTML:

❤️

js:

methods: {
increaseRatings(game) {
const rate= game.rating++;
this.game.rating = rate;
}
}

Collapse
 
lincxx profile image
Jeremy Lincoln


<span v-for="star in game.rating" @click="game.rating+=1">❤️</span>

Collapse
 
pwnchaurasia profile image
Pawan Chaurasia

My Solution for the challenge
thepracticaldev.s3.amazonaws.com/i...

Collapse
 
marinamosti profile image
Marina Mosti

It works, but it's a little overkill :) You can pass the whole 'game' object to the function, that way you don't have to .find on the array. Also, be careful with ==, as a best practice try to use === that way you won't run into very hard to understand bugs with types! :D

Collapse
 
pwnchaurasia profile image
Pawan Chaurasia

noted. Thanks

Collapse
 
neutrino2211 profile image
Mainasara Al-amin Tsowa

LOL Fallout 76 not spared here either. Nice post though, you really did not leave any stone unturned

Collapse
 
marinamosti profile image
Marina Mosti

:D Well they really messed up. Also, thanks!

Collapse
 
asuliman501 profile image
AhmedSuliman

Thanks a lot for sharing v-for. I'm still a bit confused in a few things you've done above.I am a beginner in Vue.js :) Please keep blogging .

Collapse
 
ericstolzervim profile image
Eric Stolzervim

Pass game as a parameter.
Then can have ...

methods: {
addRating(game) {game.rating +=1 }
}

So don't need "this."

Collapse
 
marinamosti profile image
Marina Mosti

This is another way to do it :)

Collapse
 
irochkaz profile image
Iryna Zaletko

please tell me, the v-key attribute should be visible in the markup?thnk u
prntscr.com/smsw3e

prntscr.com/smswmk

Collapse
 
marinamosti profile image
Marina Mosti

Hey Iryna, no it shouldn't be rendered to the markup as it is internal to Vue