'Field value is getting pushed to wrong array

In a vue app form, I have two text inputs and a file input.

What I want to happen: The text inputs should go into a fieldHeader array, and the file input should go into the fieldsArray array.

What does happen: Both text fields and the file input go into both the fieldHeader and the fieldsArray. The console.log for both arrays after clicking "add field" is

0: {field: 0, name: 'text', desc: 'text', imgOne: File}

It should be fieldHeader:

0: {field: 0, name: 'text', desc: 'text'}

and fieldsArray:

0: {imgOne: File}

I would think the v-model would push the items into the correct arrays, but that's not it. The think I am working through now is to restructure the v-for as that's the only place where I can currently see the code as mixing the two array concepts.

<template>
  <div v-for="(field, i) in fieldsArray"  :key="i" >
    <q-input
      label="Name"
      v-model="fieldHeader[i].name"
      type="text"
      dense
    />
    <q-input
      v-model="fieldHeader[i].desc"
      type="textarea"
    />
    <q-file
      v-model="fieldsArray[i].imgOne"
      @update:model-value="onSelected()"
    ></q-file>
  </div>
  <div @click="handleAddField">Add Field</div>
</template>
<script>
const handleAddFields = () => {
  let i = fieldsArray.value.length;

  const item = {
    field: i,
  };
  fieldHeader.value.push(item);
  fieldsArray.value.push(item);
  
};
</script>


Solution 1:[1]

Try like following snippet:

const { ref } = Vue
const app = Vue.createApp({
  setup () {
    const fieldsArray = ref([])
    const fieldHeader = ref([])
    const handleAddField = () => {
      fieldHeader.value.push({name: '', desc: ''});
      fieldsArray.value.push({imgOne: null});
    };
    const onSelected = () => { }
    return { fieldsArray, fieldHeader, handleAddField, onSelected }
  }
})
app.use(Quasar)
app.mount('#q-app')
<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900|Material+Icons" rel="stylesheet" type="text/css">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/quasar.prod.css" rel="stylesheet" type="text/css">
<div id="q-app">
  <div v-for="(field, i) in fieldsArray" :key="i" >
    <q-input
       label="Name"
       v-model="fieldHeader[i].name"
       type="text"
       dense
     ></q-input>
     <q-input
       v-model="fieldHeader[i].desc"
       type="textarea"
     ></q-input>
     <q-file
       v-model="fieldsArray[i].imgOne"
       @update:model-value="onSelected()"
     ></q-file>
 </div>
 <q-btn @click="handleAddField">Add Field</q-btn>
 <p>fieldsArray: {{fieldsArray}}</p>
 <p>fieldHeader: {{fieldHeader}}</p>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@3/dist/vue.global.prod.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/quasar.umd.prod.js"></script>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nikola Pavicevic