[Vuejs]-VueJs push array into array values only

1👍

EDIT: If you want to loop through elements in an Array without returning an array of the same size, you can use a forEach method to define your custom function. In your case,

this.variationChilds.forEach(el => {
  this.form.cores.push(el.old_core_id);
  this.form.cores.push(el.new_core_id);
})

should do the trick.

Original Answer: VariationChilds is an array, and you’re pushing the entire array as one element into your form.cores array, causing your problem.

this.form.cores.push(this.variationChilds); // [].push([{old_core, new_core},{old_core,new_core}]

If you want a copy of variationChilds as the value for form.cores, use this.form.cores = this.variationChilds.slice(). Alternatively, use a spread operator to push every contained element separately to the new array this.form.cores.push(...this.variationChilds)

Or, depending on your use case, you could simply push a new entity directly to this.form.cores instead of having the extra layer in between.

Leave a comment