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
$set for modifying objects in array for reactivity #4443
Comments
Please make sure to read the Issue Reporting Guidelines before opening new issues. The issue list only accepts bug reports and feature requests. Questions should be posted to the Gitter chat room, forum or StackOverflow. Thanks. You just do Also, in 2.0 Arrays are no longer augmented with |
When I do Also thank you for pointing out that Arrays are no longer augmented with $set, 2.0 onward. |
No, the actual mutation happens on the object inside the array, not the array itself, so it is not subject to the index detection caveat. |
I've made this repo to show you the problem. Please take a look at it. https://jsfiddle.net/donnyjeremiah/tfvouu61/1/ You can see here that once Note, that I have a button P.S: I've tried using |
You seem to have a misunderstanding of how JavaScript array works... the way you are using it is like an Object hash, not an Array. Mutations to Arrays in JavaScript happen through methods like Doing this is adding a new property to the Array as an Object, not pushing a new element to the Array: arr['someId'] = { ... } But this is not what Arrays are for. If you want to store things with string keys you should use an Object. And since you are adding a new property, you should use |
Yeah, I understood that an array with strings for indexes is basically an object with a property, when I was creating this repo. I didn't know that before. But there is one thing. Despite, the way or purpose I've used the array, why isn't Vue updating it? It should, shouldn't it? So since arr['someId'] is just arr.someId and can be accessed by arr['someId'] AND arr.someId, why isn't it updating? P.S: Also if I was using objects, I wouldn't be adding a property, I would be 'modifying it'. Modifying |
Oh boy. |
@simplesmiler Thank you :) So just to clarify, because I'm very restless when I don't know what it was that I did wrong, was it because that I was using |
Yes. Vue was unable to detect the addition of a new property |
Evan Almighty! So I wasn't insane after all! It's just that I wasn't sure what problem I was running into. Thank you @simplesmiler and @yyx990803! You guys have good day :) |
Vue.js version
2.1.0
Problem:
I can't update an object in my array of objects
editedPlayers[]
when I usethis.$set()
. When I try to dothis.editedPlayers.$set(id, this.editedPlayers[id].stats[type] + 1)
. I know this is wrong.editedPlayers[{stats:{goals: 12, assists:3, played: 10}}, {stats:{goals: 23, assists:7, played: 30}}]
I want to update the
stats.goals
in editedPlayers[someId]. Since $set or splice() can be used to substitute the current index value with another, I want just to modify it (ie:stats.goals
). What do I do?The text was updated successfully, but these errors were encountered: