-1
var categories= {
  "art": 352,
  "health-beauty": 358,
  "home": 372,
  "jewelry": 339,
  "kids": 320

}
for(var i in categories)
{
    console.log("name: " + i + "id: " + ?);
}

I'm trying to import name of categories and id to mongodb but having hard time how to get the ID of each value? Is this possible or I have to re arrange the JSON file by hand?

zumbamusic
  • 180
  • 1
  • 2
  • 14

1 Answers1

0

Your var i contains the property and object's properties can be accessed using an arraylike notation. so:

for(var i in categories) {
    console.log("name: " + i + "id: " + categories[i]);
}

I recommend that you always put your curly braces on the right because not doing so would yield unexpected results:

function someFunction () 
{ 
  return 
  { 
    prop: "Prop" 
  } 
}

alert(someFunction().prop); // Error, it returned undefined because something called semi-colon insertion. Read about it.
Roger
  • 2,912
  • 2
  • 31
  • 39