I would like to populate a 2D array with values using v-model
.
Here is an example of my array containing objects:
[
{
first_attribute_value: 'red',
second_attribute_value: 'medium',
third_attribute_value: 'cotton',
quantity: [
{ location_id: 32, quantity: '' },
{ location_id: 31, quantity: '' },
{ location_id: 30, quantity: '' },
{ location_id: 1, quantity: '' },
],
},
{
first_attribute_value: 'red',
second_attribute_value: 'medium',
third_attribute_value: 'leather',
quantity: [
{ location_id: 32, quantity: '' },
{ location_id: 31, quantity: '' },
{ location_id: 30, quantity: '' },
{ location_id: 1, quantity: '' },
],
},
],
Here is the function that returns the testvar array:
setVariancesJSON(){
this.firstattrs.forEach(first=>{
if(this.secondattrs.length && this.thirdattrs.length){
this.secondattrs.forEach(second=>{
this.thirdattrs.forEach(third=>{
this.testvar.push({
first_attribute: this.attrnames[0],
first_attribute_value: first,
second_attribute: this.attrnames[1],
second_attribute_value: second,
third_attribute: this.attrnames[2],
third_attribute_value: third,
quantity:this.freeProductQuantity
})
})
})
}
else if(this.secondattrs.length && !this.thirdattrs.length){
this.secondattrs.forEach(second=>{
this.testvar.push({
first_attribute: this.attrnames[0],
first_attribute_value: first,
second_attribute: this.attrnames[1],
second_attribute_value: second,
third_attribute: null,
third_attribute_value:null,
quantity:this.freeProductQuantity
})
})
}
else{
this.testvar.push({
first_attribute: this.attrnames[0],
first_attribute_value: first,
second_attribute:null,
second_attribute_value: null,
third_attribute: null,
third_attribute_value:null,
quantity:this.freeProductQuantity
})
}
})
}
I attempted to assign values to the quantity
attribute within each quantity
array
This was my approach:
<tr v-for="(attr,index) in testvar " :key="index">
<td class="col-2 text-center" v-for="(warehouse,index2) in warehouses" :key="index2">
<input v-model="testvar[index].quantity[index2].quantity">
</td>
</tr>
However, all input fields display the same value for each column instead of unique values. It seems to always read "index" with the same value during each iteration.