0

I'm trying to increase the quantity count of an existing object in my shopping cart instead of adding it again. Objects look like this:

{
id: 2​​,
name: "Squash Supreme",
selectedSize: "38",
price: 75,
quantity: 1
}

With .includes() I tried to verify if the exact object already exists in the array, but couldn't. Tumbled over .find() .some() etc. but none of the examples took two key-value pairs.

I need to check if an object with two identical key-value pairs (id and selectedSize) is already present.

Any suggestions? (I'm using react, with functional components, Array is in an useState.)

After that I can tackle the increment functionality ;) Thanks in advance!

aturan23
  • 4,798
  • 4
  • 28
  • 52
ARu
  • 1
  • *"...but none of the examples took two key-value pairs."* That doesn't matter, what the `find` callback does is completely up to you. The [accepted answer](https://stackoverflow.com/a/35398031/157247) to the linked dupetarget is `myArray.find(x => x.id === '45').foo;`. In your case, it would be `const entry = myArray.find(x => x.id === theId && x.selectedSize == theSize);` – T.J. Crowder Mar 03 '20 at 08:42
  • There surely may be certain suggestions ***to help you progress with your current attempt*** if sample array (to play with) along with desired result were provided. Until neither of those were given, odds aren't too high. – Yevhen Horbunkov Mar 03 '20 at 08:45
  • Thanks Crowder! I knew there was an easy solution :) – ARu Mar 03 '20 at 16:28

0 Answers0