如果我误解了这个问题,请告诉我哪里出错了。
我假设25、15、10和5是预先定义的。这是对的吗?我没有在你的问题中看到这一点。
// Defined lengths
const lengths = [25, 15, 10, 5];
// Some example cart corresponding to how many of each length customer has (this is from your example)
const cart = {25: 1, 15: 1, 5: 1}
for (let length of lengths) {
//Check for each length in lengths array
let counter = 0;
for (let item in cart) {
// Add the counter if there is enough in cart
counter += Math.floor(item * cart[item] / length);
}
// I am console logging like you showed, but you can do whatever
console.log(`${counter} ${length}m`)
}
输出:
1 25m
2 15m
3 10m
9 5m