[Vuejs]-Insert or update object in Array Javascript

0👍

You need to use the given index param to update particular object fields in the additionalFields array.

adaptAdditionalTitleForm(obj) {
   if(!obj.index || (obj.index && obj.index>=this.form.additionalFields.length)){
       this.form.additionalFields.push({title:object.value,description:''}) 
   }else if(obj.index) {
   this.form.additionalFields[obj.index].title= obj.value;
  }
},

adaptAdditionalDescriptionForm(obj) {
 if(!obj.index || (obj.index && obj.index>=this.form.additionalFields.length)){
   this.form.additionalFields.push({description:obj.value,title:''})
  }else if(obj.index){
     this.form.additionalFields[obj.index].description=obj.value;
  }
}

Also passing multiple arguments to emit doesn’t work so you need to wrap them in an object.

updateAdditionalTitleForm(value, index) {
    this.$emit('updateAdditionalTitle', {value,index});
},
updateAdditionalDescriptionForm(value, index) {
   this.$emit('updateAdditionalDescription',{value,index});
}

Leave a comment