在计算方法中显示混合数组
Displaying mixed arrays in computed method
我有一个计算方法可以显示一些数据:
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;
}
我想按顺序显示元素:来自 'names' 数组的元素 + 来自 'numbers' 数组的元素:'Item: item1 - Amount: 1'
.
你可以这样映射它:
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(', ')
}
我有一个计算方法可以显示一些数据:
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;
}
我想按顺序显示元素:来自 'names' 数组的元素 + 来自 'numbers' 数组的元素:'Item: item1 - Amount: 1'
.
你可以这样映射它:
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(', ')
}