我只想在所有檢查都通過后更新物件陣列。我有代表所有文章的物件陣列和所有可用庫存的其他物件陣列。
我想檢查是否所有文章都有庫存,那么我只想更新庫存陣列。這是我的代碼:
const articles = [{
"id": "1",
"quantity": "4"
}, {
"id": "2",
"quantity": "8"
}, {
"id": "4",
"quantity": "1"
}];
let stock = [
{
"id": "1",
"stock": "6"
},
{
"id": "2",
"stock": "6"
},
{
"id": "3",
"stock": "2"
},
{
"id": "4",
"stock": "2"
}
];
articles.map(article => {
stock.map(inventItem => {
if(inventItem.id === article.id){
if(article.quantity <= inventItem.stock){
inventItem.stock = inventItem.stock - article.quantity;
} else {
console.log('error');
throw error;
}
}
});
});
這個解決方案的問題是它會更新庫存,id = 1即使id = 2還不夠。我正在嘗試檢查所有商品是否有足夠的數量并在庫存陣列中更新(減去)它們。所以在這種情況下,代碼將像這樣更新股票陣列:
stock = [
{
"id": "1",
"stock": "2"
},
{
"id": "2",
"stock": "6"
},
{
"id": "3",
"stock": "2"
},
{
"id": "4",
"stock": "2"
}
];
有人可以建議我如何解決這個問題嗎?
uj5u.com熱心網友回復:
您可以使用Array.prototype.find(). 注意:該片段假定所有文章都有一個"id"屬性,它不檢查"art_id".
const articles = [{
"id": "1",
"quantity": "4"
}, {
"id": "2",
"quantity": "8"
}, {
"id": "4",
"quantity": "1"
}];
let stock = [{
"id": "1",
"stock": "6"
},
{
"id": "2",
"stock": "6"
},
{
"id": "3",
"stock": "2"
},
{
"id": "4",
"stock": "2"
}
];
// Use Array.prototype.some() to check for items that don't have enough stock
const haveEnough = !articles.some(article =>
// Turns "1" into 1
parseInt(
// Find the stock with the same id as the article
stock.find(stock => stock.id === article.id).stock
) <
parseInt(article.quantity))
console.log(haveEnough)
然后你可以stock更新
if (haveEnough) {
// Do something
}
uj5u.com熱心網友回復:
我假設陣列中的art_id鍵articles應該是id.
不幸的是,您不能在一個回圈中完成所有操作,而是必須先遍歷所有文章以了解它們的庫存是否足夠,然后再次遍歷它們以更改現有庫存:
const articles = [
{
id: '1',
quantity: '4',
},
{
id: '2',
quantity: '5',
},
{
id: '4',
quantity: '1',
},
];
let stock = [
{
id: '1',
stock: '6',
},
{
id: '2',
stock: '6',
},
{
id: '3',
stock: '2',
},
{
id: '4',
stock: '2',
},
];
// Are there any articles that do not have enough stock
const outOfStockArticles = articles.find((article) => {
const articleStock = stock.find((stock) => stock.id === article.id);
return Number.parseInt(article.quantity) > Number.parseInt(articleStock.stock);
});
// If there are no out of stock articles - change existing stock values
if (!outOfStockArticles) {
articles.forEach((article) => {
const articleStock = stock.find((stock) => stock.id === article.id);
articleStock.stock = Number.parseInt(articleStock.stock) - Number.parseInt(article.quantity);
});
}
uj5u.com熱心網友回復:
您的文章陣列中存在不一致,我想每篇文章都有一個“art_id”,而不是一些“id”和一些“art_id”。
您可以使用 Array.prototype.find 函式簡單地找到您的文章,然后通過isAvailable函式檢查庫存串列中的文章數量是否正確。
現在你可以使用 Array.prototype.every 函式來做你想要的!只有當陣列的每個元素都滿足條件時,它才會回傳 true,在我們的例子中是isAvailable函式。
這是一個簡單的小提琴:
const articles = [{
"art_id": "1",
"quantity": "4"
}, {
"art_id": "2",
"quantity": "8"
}, {
"art_id": "4",
"quantity": "1"
}];
const stock = [{
"id": "1",
"stock": "6"
},
{
"id": "2",
"stock": "6"
},
{
"id": "3",
"stock": "2"
},
{
"id": "4",
"stock": "2"
}
];
const isAvaiable = (article) => {
return stock.find(element => element.id === article.art_id).stock >= article.quantity;
}
if (articles.every(isAvaiable)) {
console.log("I can update")
} else {
console.log("I cannot update")
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/451010.html
標籤:javascript 数组 打字稿
上一篇:移位陣列元素java
