gpt4 book ai didi

javascript - 在计算方法中显示混合数组

转载 作者:行者123 更新时间:2023-11-30 14:47:27 26 4
gpt4 key购买 nike

我有一个计算方法可以显示一些数据:

  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;
}

我想按顺序显示元素:来自“名称”数组的元素 + 来自“数字”数组的元素:'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(', ')
}

关于javascript - 在计算方法中显示混合数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48693956/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com