如何根据 material 的长度以编程方式计算数量?
How can I programatically calculate quantities based on length of material?
我正在计算如果我将一个较大尺寸的卷切成多个较小尺寸的卷,我可以拥有的 material 卷的数量。
例如,如果我有 1 个 25m 卷,我可以将其切成 1 个 15m 卷、2 个 10m 卷和 5 个 5m 卷。所以我希望我的数量看起来像:
- 1 25 米
- 1 15 米
- 2 10 米
- 5 5米
现在,我还可以拥有任何其他数量的现有数量,例如 1 卷 25m、1 卷 15m 和 1 卷 5m。然后它看起来像:
- 1 25 米
- 2 15 米
- 3 10 米
9 5m
for (let i = 0; i < this.sizes.length; i++) {
const size = this.sizes[i];
for (let j = 0; j < this.cart.items.length; j++) {
const item = this.cart.items[j];
if (item.sizeId === size.id) {
size.quantity -= item.quantity;
}
size.amountOfMaterial = size.quantity * size.length;
}
}
我设置了第一个循环,以根据他们购物车中已有的商品获得 material 的正确数量和数量。我卡在下一部分了。
编辑:下面的答案最终让我想到了这个:
calculateQuantities() {
let quantities = {};
for (let i = 0; i < this.sizes.length; i++) {
const size = this.sizes[i];
for (let j = 0; j < this.cart.items.length; j++) {
const item = this.cart.items[j];
if (item.sizeId === size.id) {
size.quantity -= item.quantity;
}
}
size.actualQuantity = size.quantity;
let counter = 0;
for (let j = 0; j < this.sizes.length; j++) {
const otherSize = this.sizes[j];
counter += Math.floor(otherSize.length * otherSize.quantity / size.length)
}
console.log(`${counter} ${size.length}m`);
quantities[size.length] = counter;
}
for (let i = 0; i < this.sizes.length; i++) {
this.sizes[i].quantity = quantities[this.sizes[i].length];
}
}
如果我误解了这个问题,请让我知道哪里做错了。
我假设 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
我正在计算如果我将一个较大尺寸的卷切成多个较小尺寸的卷,我可以拥有的 material 卷的数量。
例如,如果我有 1 个 25m 卷,我可以将其切成 1 个 15m 卷、2 个 10m 卷和 5 个 5m 卷。所以我希望我的数量看起来像:
- 1 25 米
- 1 15 米
- 2 10 米
- 5 5米
现在,我还可以拥有任何其他数量的现有数量,例如 1 卷 25m、1 卷 15m 和 1 卷 5m。然后它看起来像:
- 1 25 米
- 2 15 米
- 3 10 米
9 5m
for (let i = 0; i < this.sizes.length; i++) { const size = this.sizes[i]; for (let j = 0; j < this.cart.items.length; j++) { const item = this.cart.items[j]; if (item.sizeId === size.id) { size.quantity -= item.quantity; } size.amountOfMaterial = size.quantity * size.length; } }
我设置了第一个循环,以根据他们购物车中已有的商品获得 material 的正确数量和数量。我卡在下一部分了。
编辑:下面的答案最终让我想到了这个:
calculateQuantities() {
let quantities = {};
for (let i = 0; i < this.sizes.length; i++) {
const size = this.sizes[i];
for (let j = 0; j < this.cart.items.length; j++) {
const item = this.cart.items[j];
if (item.sizeId === size.id) {
size.quantity -= item.quantity;
}
}
size.actualQuantity = size.quantity;
let counter = 0;
for (let j = 0; j < this.sizes.length; j++) {
const otherSize = this.sizes[j];
counter += Math.floor(otherSize.length * otherSize.quantity / size.length)
}
console.log(`${counter} ${size.length}m`);
quantities[size.length] = counter;
}
for (let i = 0; i < this.sizes.length; i++) {
this.sizes[i].quantity = quantities[this.sizes[i].length];
}
}
如果我误解了这个问题,请让我知道哪里做错了。 我假设 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