[英]How to decrement items in array Incrementally based on the given quantity in Javascript
我有這個示例對象數組
mangoes:[
{ quantity:5},
{ quantity:8},
{ quantity:13},
{ quantity:4}
]
當我刪除 x 個芒果時,應該從數組中的第一個元素中減去那個 x,如果那個 x 超過第一個元素,那么它應該減少數組中第二個項目的剩余數量......等等。
也就是說,我需要從數組中的第一個開始減少到第二個(如果超過),再到第三個等等。
例如,如果我買了 2 個芒果,它應該在第一個數組元素中減去 2,得到的芒果數組應該是
mangoes:[
{ quantity:3},
{ quantity:8},
{ quantity:13},
{ quantity:4}
]
另一方面,如果我會買 7 個芒果,它應該從第一個數組元素中減少所有 5 個,然后從第二個元素中刪除 2 個芒果......因此最終的數組將如下所示
mangoes:[
{ quantity:0},
{ quantity:6},
{ quantity:13},
{ quantity:4}
]
通過使用 Javascript,我該如何實現?
我嘗試過的
我在下面嘗試過這樣的方法,它僅適用於 x 較小時的第一個元素(情況),但對於其他情況則不起作用;
var x = 2
var done = false
mangoes.forEach(function (item,i) {
if(mangoes[i].quantity>=x && !done){
item.quantity = mangoes[i].quantity - x
done = true
}
})
const takeMangoes = (num, from) => from.map(x => { const take = Math.min(x.quantity, num); num -= take; // Original way, keep all properties, don't mutate original //return {...x, quantity: x.quantity - take}; // Pick from below: // New way 1, mutate original in place x.quantity = x.quantity - take; return x; // New way 2, limit to OWN properties return Object.getOwnPropertyNames(x).reduce((a, c) => { a[c] = x[c]; if (c === 'quantity') { a[c] -= take; } return a; }, {}); }).filter(x => x.quantity > 0); console.log(takeMangoes(2, [ {quantity: 5}, {quantity: 8}, {quantity: 13}, {quantity: 4}, ])); console.log('------------------------'); console.log(takeMangoes(7, [ {quantity: 5}, {quantity: 8}, {quantity: 13}, {quantity: 4}, ]));
您可以通過這種方式使用forEach
實現這一目標。 但我不推薦它,因為即使發現一個數量大於要扣除的值,它仍然會循環整個項目列表,因為你無法跳出forEach
循環
var mangoes = [ { quantity:0}, { quantity:6}, { quantity:13}, { quantity:4} ]; var x = 2 var done = false mangoes.forEach(function (item,i) { if(item.quantity< x){ item.quantity = 0; x = x- item.quantity; }else{ item.quantity = item.quantity-x; x=0; } }); console.log(mangoes)
但是,我建議使用for..of..loop
以便您可以有條件地跳出循環,即如果發現數量大於要扣除的數量,則只需扣除並跳出循環。 無需進一步迭代。
var mangoes = [ { quantity:5}, { quantity:6}, { quantity:13}, { quantity:4} ]; var x = 1 var done = false for(let i of mangoes){ if(i.quantity >= x){ i.quantity = i.quantity -x; x = x - i.quantity; break; }else{ x = x - i.quantity; i.quantity = 0; } } console.log(mangoes)
let x = 8;
const newMangoes = mangoes.map((mango) => {
if (!x) return mango;
if (x <= mango.quantity) {
newMango = {quantity: mango.quantity - x};
x = 0;
return newMango;
} else {
x = x - mango.quantity;
return {quantity: 0};
}
});
您可以在closure
內關閉並生成所需的結果。
var mangoes=[ { quantity:5},{ quantity:8},{ quantity:13},{ quantity:4}]; var toReduce = 5; var result = mangoes.map((num=>({quantity})=>(r = Math.min(quantity,num),num=num-r,({quantity:quantity-r})))(toReduce)); console.log(result);
var getMangoes = function(requested){
var remaining = requested;
var supplied = 0;
for(var i = 0; i < mangoes.length() && supplied < requested; i++){
if(mangoes[i].quantity >= remaining){
mangoes[i].quantity -= remaining;
supplied += remaining;
}
else {
remaining -= mangoes[i].quantity;
supplied += mangoes[i].quantity;
}
}
return(supplied);
};
var mangoesIWant = 13;
var mangoesIGot = getMangoes(mangoesIWant);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.