I am currently working with an array called cars
, which contains names of cars as well as a property called starred
. My goal is to toggle the value of starred
between true and false for each car, ensuring that only one car can have starred
set to true at a time. To achieve this, I created a method named setStarred
. Within this method, I use map
to set all other cars' starred
properties to false before setting the selected car's property to true. However, I am facing an issue where I can successfully set the selected car's property to true, but I struggle to set it back to false.
If you would like to review the code, please visit this Codepen link
This code presents a functioning example:
new Vue({
el: "#app",
data() {
return {
cars: [{
name: "Toyota",
starred: false
},
{
name: "BMW",
starred: false
},
{
name: "Ford",
starred: false
}
]
};
},
methods: {
setStarred(index) {
this.cars.map((i) => {
i.starred = false;
});
this.cars[index].starred = !this.cars[index].starred;
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="304645554459564970011e051e0104">[email protected]</a>/dist/vuetify.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a9dfdcccddc0cfd0e998879c87989d">[email protected]</a>/dist/vuetify.min.css" rel="stylesheet" />
<div id="app">
<v-app id="inspire">
<v-container>
<v-layout justify-center column>
<v-flex xs6 v-for="(car,index) in cars" :key="index">
<h2>{{car.name}}
<v-icon :color="car.starred ? 'primary': '' " @click="setStarred(index)">star_border
</v-icon>
</h2>
</v-flex>
</v-layout>
</v-container>
</v-app>
</div>
In essence, my aim is to reset the selected car's starred
property back to false. Any assistance on this matter would be greatly appreciated. Thank you