How can you delete a property/key from a Vue.js data object (i.e. associative array) like this:
var vm = new Vue({ data: { users: { foo : { firstName: ..., lastName: ... }, bar : { firstName: ..., lastName: ... } } }, methods: { someFunction : function () { // how to remove `users.foo`? } } });
Googling around, I found these two ways, but both don't work:
delete this.users.foo;
is not updating the DOMthis.users.splice('foo', 1);
is not working at all (probably only works on arrays, not on objects)In JavaScript, there are 2 common ways to remove properties from an object. The first mutable approach is to use the delete object. property operator. The second approach, which is immutable since it doesn't modify the original object, is to invoke the object destructuring and spread syntax: const {property, ...
The delete operator is used to remove these keys, more commonly known as object properties, one at a time. The delete operator does not directly free memory, and it differs from simply assigning the value of null or undefined to a property, in that the property itself is removed from the object.
To make a component delete itself in Vue. js, we can use the this. $destroy method. to add the close method to call this.
The answer is:
Vue.delete(users, 'foo');
It took me a while to find it, that's why I'm posting it here ;-)
https://github.com/vuejs/vue/issues/3368#issuecomment-236642919
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With