1

Suppose I have this code:

var simple = {
Cicatrix: ["Rock", "Bottom", "Stunner"],
Yen_Sid: ["Pirate", "Heroes", "Power"],
};

Calling Rock, Bottom, Stunner and etc. is easy.

document.write(simple.Cicatrix[0]);

However, what if i wanted to get the word Cicatrix and Yen_Sid? I want them to be called because I am going to use them as menus.

Also is it possible to have spaces in array names? Like Yen_Sid turns to Yen Sid. Or should I do a .replace?

Selim
  • 197
  • 1
  • 11
  • 1
    Don't do this: split your data and presentation. It will make your life much easier later, if you need to localize things or change the names. – ssube Sep 08 '15 at 16:09

5 Answers5

1

Simply use a for loop to go over simple with a replace.

var simple = {
Cicatrix: ["Rock", "Bottom", "Stunner"],
Yen_Sid: ["Pirate", "Heroes", "Power"],
};

for(x in simple)
  console.log(x.replace('_',' '));
depperm
  • 10,606
  • 4
  • 43
  • 67
1

This allows you to iterate over an object's properties

  for (var property in object) {
        if (object.hasOwnProperty(property)) {
            // do stuff
        }
    }

It is not possible to have whitespace as you suggest in object notation property names unless you use double quotes like

obj = {"prop 1": [1,2,3]}

var t = obj["prop 1"]
1

For your first question, getting keys as strings is possible using Object.keys() method. I am not going to write examples here since the documentation I provided earlier has plenty of snippets that shows you how to use it. Other solution would be to use for loop to loop over the keys as Matthew Christianson pointed out in other answer.

And yes, you can have keys that are named with space, but when you're calling them be sure to do it using object.[property] notation, as in:

var x = {
    "a b c" : 1234
}

console.log(x["a b c"]); // will log "1234" to Console!

I wouldn't suggest you using names with spaces since your code gets a bit messy using bracket notation, in my opinion. But you can make another object whose keys are keys from your primary object, and values are correctly spelled names, as in:

var names = {
   YenSid : "Yen Sid",
   SomeName : "Some name",
   SomeOtherName : "Some other name"
}

var properObject = {
   YenSid = 123,
   SomeName = 456,
   SomeOtherName = 789
}

And to get a correct name just use names.YenSid

Mirza
  • 213
  • 2
  • 8
1

Yes, you can have spaces in property names, you just need a delimiter around it:

var simple = {
  Cicatrix: ["Rock", "Bottom", "Stunner"],
  "Yen Sid": ["Pirate", "Heroes", "Power"],
};

However, if you want to loop out the items in a specific order, then you don't want them as object properties, you want them as items in an array. The properties in an object doesn't have a specific order, so when you loop out properties the order depends on the implementation in the browser, and different browsers will give you the properties in different order.

var simple = [
  { name: "Cicatrix", items: ["Rock", "Bottom", "Stunner"] },
  { name: "Yen Sid", items: ["Pirate", "Heroes", "Power"] }
];

To loop through the items and subitems you just need a nested loop:

for (var i = 0; i < simple.length; i++) {
  // menu item is in simple[i].name
  // subitems are in simple[i].items:
  for (var j = 0; j < simple[i].items.length; j++) {
    // subitem is in simple[i].items[j]
  }
}
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • Ok so you really need to put name: and items: as such. I'll test this first and once all goes well, I'll accept this. – Selim Sep 08 '15 at 16:28
  • @Selim: There are other possibilities, but this is the simplest approach to get the items in a speicific order. The property names `name` and `items` in the object can naturally be changed to whatever you like. – Guffa Sep 08 '15 at 16:33
  • Tested it and worked properly with the code that I have now. Thank you for this answer. The other stuff that I have read requires me to rewrite all the codes that I have finished. Thanks again! – Selim Sep 08 '15 at 16:59
1

Try this, its an example of how to print out the array to a string.

var text = "";
$.each(simple, function(k) {
    text = text + "// " + k + " contains: ";
    for ( var i = 0, l = $(this).length; i < l; i++ ) {    
            text = text + $(this)[i] + ", ";
    }
});

console.log(text);
Jake Bown
  • 411
  • 4
  • 11