2 回答

TA貢獻1786條經驗 獲得超13個贊
使用 array.reduce() 將是一種更好、更清潔的方法。
let arr = [{ Item: "", Quantity: 2 }, { Item: "B", Quantity: 7 }, { Item: "", Quantity: "" }]
const groupBy = arr.reduce(function (total, obj)
{
total += (obj.Item + ":" + (obj.Quantity + " , "));
return total;
}, 0);

TA貢獻1827條經驗 獲得超8個贊
您可以映射這些值并加入它們。
var array = [{ Item: "", Quantity: 2 }, { Item: "B", Quantity: 7 }, { Item: "", Quantity: "" }],
string = array
.filter(({ Item, Quantity }) => Item || Quantity)
.map(({ Item, Quantity }) => `${[Item ? Item : "", Quantity ? Quantity: ""].join(': ')}`)
.join(', ');
console.log(string);
添加回答
舉報