I need to implement a dynamic array data adding functionality in JavaScript/Vue.js.
Adding data to an array is straightforward:
methods: {
add: function add(e) {
e.preventDefault();
if (!this.newName) return;
this.config.names.firstnames.push(this.newName);
this.newName = '';
},
}
However, I want the array property this.config.names.firstnames
to be dynamic.
When this.type == "firstnames"
, the data should be added to this.config.names.firstnames
. If this.type == "lastnames"
, the data should be added to this.config.names.lastnames
. Thus, the target array should be determined by the value of this.type
.
What is the most elegant way to achieve this?