JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.10/vue.js"></script>
<div id="app">
<div v-for="(v, k) in model">
<input v-model="model[k]">
{{ k }}
</div>
<pre>{{ obj }}</pre>
</div>
CSS
* {
font-family: monospace;
}
JavaScript
new Vue({
el: '#app',
data: () => ({
obj: {
a: 'hello, world!!',
b: {
value: '666',
},
},
obj2: [
{ model: 'a' } ,
{ model: 'b.value' },
{ model: 'c.d.e.f.g' },
],
}),
computed: {
model() {
return new Proxy(Object.fromEntries(this.obj2.map(n => [ n.model, null ])), {
get: (target, prop) => typeof prop === 'string'
? prop.split('.').reduce((p, c) => p?.[c], this.obj)
: target[prop],
set: (target, prop, value) => {
const keys = prop.split('.');
const key = keys.pop();
const obj = keys.reduce((p, c) => (!p.hasOwnProperty(c) && this.$set(p, c, {}), p[c]), this.obj);
this.$set(obj, key, value);
return true;
},
});
},
},
});