以计算方法显示混合数组

时间:2018-02-08 20:10:19

标签: javascript vue.js computed-properties

我有一个允许我显示一些数据的计算方法:

  productsSpecification() {
    var names = [];
    var numbers = [];
    this.cart.items.forEach(function(item) {
      names += "Item: " + item.product.name + " -";
      numbers += " Amount: " + item.quantity + ", ";
    });
    var together = names + numbers;
    return together;
  }

我想按顺序显示元素:来自' name'数组+元素来自'数字'数组:'Item: item1 - Amount: 1'

1 个答案:

答案 0 :(得分:2)

您可以像这样映射:

productsSpecification() {
  return this.cart.items.map(function(item) {
    return `Item: ${item.product.name} - Amount: ${item.quantity}`; // or "Item: " + item.product.name + " - Amount: " + item.quantity;
  }).join(', ')
}