-1

Totally stumped here.

I have the following object:

var stStatus = new StockStatus({
  "3":{
    "is_in_stock":false,
    "custom_status_icon":"",
    "custom_status":"Out of Stock",
    "product_id":"9",
    "stockalert":""
  },
  "5":{
    "is_in_stock":true,
    "custom_status_icon":"",
    "custom_status":"",
    "product_id":"10"
   },
  "88":{
    "is_in_stock":true,
    "custom_status_icon":"",
    "custom_status":"",
    "product_id":"296"
    }
});

that is being dynamically created, so I don't know the 3, 5, or 88 numbers before hand. I'm trying to figure out how to get the the 3,5,88 and their tied is_in_stock and product_id.

Trying to get it to basically only console.log the product_id and 3,5,88 numbers if is_in_stock is false

Totally stumped.

Octoxan
  • 1,949
  • 2
  • 17
  • 34

2 Answers2

2

You can iterate over Object.keys.

var keys = Object.keys(stStatus)

should give you an array containing all the keys in this instance. You can then use the result like this:

stStatus[keys[0]]

to access the first field; analogous for the rest.

Kamen Minkov
  • 3,324
  • 1
  • 14
  • 21
1

You can use Object.keys to get all the keys in an array.

Then use forEach to loop through the array and match the key with the object.

var m ={
  "3":{
    "is_in_stock":false,
    "custom_status_icon":"",
    "custom_status":"Out of Stock",
    "product_id":"9",
    "stockalert":""
  },
  "5":{
    "is_in_stock":true,
    "custom_status_icon":"",
    "custom_status":"",
    "product_id":"10"
   },
  "88":{
    "is_in_stock":true,
    "custom_status_icon":"",
    "custom_status":"",
    "product_id":"296"
    }
}

var _keyArray = Object.keys(m)
_keyArray.forEach(function(item){
 document.write('<pre>'+(m[item]['is_in_stock'])+'</pre>')

JSFIDDLE

brk
  • 48,835
  • 10
  • 56
  • 78
  • 1
    I've never understood the appeal of prefixing variable names with underscore. Why do you use it? (you're not wrong, I just don't get it) – evolutionxbox Jul 13 '16 at 13:49
  • 1
    Underscore in variable name, `document.write`, jsFiddle... SO has stack snippets, which support `console.log`. – Cerbrus Jul 13 '16 at 13:50
  • @evolutionxbox This is also to represent a private variable, Check this [LINK](http://javascript.about.com/od/hintsandtips/a/dollar-and-underscore.htm) – brk Jul 13 '16 at 13:54
  • This is working excellently. I have this fork here https://jsfiddle.net/fq8qc17r/1/ where I'm getting almost what I need. But can't seem to log/write the first number they're contained in.. – Octoxan Jul 13 '16 at 13:55
  • @Octoxan not sure what you mean by *first number*. I am assuming you are referring to the key that is 3,5 88 and so on. If it is so then check this https://jsfiddle.net/fq8qc17r/2/ – brk Jul 13 '16 at 13:58
  • What is new new StockStatus – brk Jul 13 '16 at 14:09
  • Why fake private variables with a naming convention when JS has them? Either use a closure, or let/const. – evolutionxbox Jul 13 '16 at 14:10
  • Thanks. I got it figured out with a combination of this and some Magento PHP stuff now. =] – Octoxan Jul 13 '16 at 14:11
  • @user2181397 Trying to learn how to nest this now. https://jsfiddle.net/fq8qc17r/4/ Trying to write/log every "products" key that is set to false. If you wouldn't mind taking a look – Octoxan Jul 13 '16 at 15:22