[Vuejs]-When pushing objects to an array the new object shows up like [[object],[object],[object]] instead of [object, object, object]

1👍

If you use push, you consider pushing a single element. If you want to add the elements of another array, use the concat method

1👍

You must use find instead of filter.

addToCart(id) {
  var productToAdd = this.items.find(item => item.id === id);
  if (typeof(productToAdd) !== 'undefined') {
    this.shoppingCart.push(productToAdd);
  }
}
👤Pierre

1👍

This is pretty straightforward

//Assuming that 
 this.items= [ [ { "id": 2, "category": "Snack", "name": "Slanty", "price": "50", "image": "https://www.beautifulworld.com/wp-content/uploads/2016/09/K2-Mountain.jpg" } ], [ { "id": 6, "category": "Snack", "name": "Slanty", "price": "50", "image": "https://www.beautifulworld.com/wp-content/uploads/2016/09/K2-Mountain.jpg" } ] ]

this.shoppingCart=[];
this.items.forEach(item => {
 this.shoppingCart.push(item[0]);
});
console.log(this.shoppingCart) // this will be your resultant array

Leave a comment