0

I have a script which has an array called metaData that includes product IDs and quantities. It looks like this:

"PRD 6060BNDISHRT-CHARCOAL-SMALL": 3, "PRD 6060BNDISHRT-CHARCOAL-MEDIUM": 3, "PRD 6060BNDISHRT-CHARCOAL-LARGE": 4, "PRD 6060BNDISHRT-CHARCOAL-XL": 3, "PRD 6060BNDISHRT-ALLOY-SMALL": 0, "PRD 6060BNDISHRT-ALLOY-MEDIUM": 2, "PRD 6060BNDISHRT-ALLOY-LARGE": 1, "PRD 6060BNDISHRT-ALLOY-XL": 3, "PRD 6060BNDISHRT-BLACK-SMALL": 1, "PRD 6060BNDISHRT-BLACK-MEDIUM": 0, "PRD 6060BNDISHRT-BLACK-LARGE": 0, "PRD 6060BNDISHRT-BLACK-XL": 4, "PRD 6060BNDISHRT-CABERNET-SMALL": 2, "PRD 6060BNDISHRT-CABERNET-MEDIUM": 2, "PRD 6060BNDISHRT-CABERNET-LARGE": 3, "PRD 6060BNDISHRT-CABERNET-XL": 2, "PRD 6060BNDISHRT-NAVY-SMALL": 3, "PRD 6060BNDISHRT-NAVY-MEDIUM": 3, "PRD 6060BNDISHRT-NAVY-LARGE": 0, "PRD 6060BNDISHRT-NAVY-XL": 3

This code:

  var stockChicago = metaData;
  alert(stockChicago);

results in the above list (or as much as will fit in the alert box).

Now, if I do this, I should get the result of the value for that key (it's the second one on the list), right?:

  var stockChicago = metaData["PRD 6060BNDISHRT-CHARCOAL-MEDIUM"];
  alert(stockChicago);

I expect the response to be "3," but instead, I just get "undefined." I've tried it without the quotes and have searched everywhere I can think of to figure out what I'm doing wrong. Like I said, I figure it's something very simple, but for the life of me I can't figure it out!

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
Dascalargo
  • 55
  • 5
  • 1
    Sounds like it's a JSON string, not an object – CertainPerformance Apr 29 '20 at 02:29
  • if you console.log metaData, what does it look like – afghanimah Apr 29 '20 at 02:29
  • Depends what the data *actually* is. It's not an array, though. Might be better to log it or inspect it in the debugger. – Dave Newton Apr 29 '20 at 02:30
  • @Dascalargo it should be an object or array of object for example `const metaData = { "PRD 6060BNDISHRT-CHARCOAL-SMALL": 3}` Then you can `const stockChicago = metaData["PRD 6060BNDISHRT-CHARCOAL-MEDIUM"]; alert(stockChicago);` – Chau Giang Apr 29 '20 at 03:47
  • Thanks for trying guys. I am in way over my head here. Was hoping I could figure this out, but I don't think that's going to happen. – Dascalargo Apr 29 '20 at 04:30

0 Answers0