Home > Net >  Update only after all the checks are passed
Update only after all the checks are passed

Time:03-29

I want to update array of object only after all the checks are passed. I have array of object representing all the articles and other array of object all the available stock.

I want to check the if all the articles are in stock then only I want to update the stock array. Here is my code:

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;
      }
    }
   });
 });

Problem with this solution is that it updates stock for id = 1 even though id = 2 is not enough. I am trying to check if all the articles are in stock are in enough quantity and update(minus) them in stock array. So in this case code will update stock array like this:

stock = [
    {
      "id": "1",
      "stock": "2"
    },
    {
      "id": "2",
      "stock": "6"
    },
    {
      "id": "3",
      "stock": "2"
    },
    {
      "id": "4",
      "stock": "2"
    }
  ];

Can someone suggest how can I fix this?

CodePudding user response:

You can use Array.prototype.find(). Note: The snippet assumes that all articles have an "id" property, it doesn't check for "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)

You can then update the stock in

if (haveEnough) {
 // Do something
}

CodePudding user response:

I am assuming that the art_id key in the articles array should be id.

Unfortunately you can't do it all in one loop and you will instead have to loop through all the articles first to find out if their stock is enough and then loop though them again to change the existing stock:

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);
  });
}

CodePudding user response:

There is an inconsistency in your articles array, I suppose that every articles has an "art_id", and not some an "id" and some an "art_id".

You can simply find your article using the Array.prototype.find function, and then by the isAvailable function check that an article is present in the right quantity in the stock list.

Now you can use the Array.prototype.every function that do exactly what you want! It will return true only if every element of the array satisfy the condition, in our case the isAvailable function.

Here a simple fiddle:

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")
}

  • Related