-5

I'm building a site that will serve as a database for a mobile game. The game has several items which will be in the database, that have names that contain apostrophes. This is of course no problem when those items are the values of an object property, but sometimes I need them to be an object property itself, I'm wondering what would be a good way to handle this?

I have a feeling I should convert the DB to JSON, but I'm quite new to this kind of thing and haven't ventured far enough out yet to try that, but would love to hear your opinions.

In addition to apostrophes, many items have spaces in the names("Chieftain's Blade", for example) and right now I'm just using an underscore for a space, with plans on using Regex to replace the _ with a blank space when I need to output that object to the DOM. I thought I could maybe do the same with the apostrophe, where I could use some other symbol and then replace it with Regex, but is there a better way?

My primary question is about the apostrophe, but I'd really appreciate some overall guidance in general about how to organize a database that will contain around 1000 objects, each with around 10-15 properties each. Should I just leave it in my JS file or is there a better solution?

Chirpizard
  • 291
  • 3
  • 17
  • What you're asking is unclear, but appears to be at least two completely distinct questions: Apostrophes in property names (although it's unclear where you mean this - column names? object property names in JavaScript?), and how to organize the database. On SO, you must only ask **one** question per question. – T.J. Crowder Dec 18 '15 at 07:22
  • In general, there's no need to change anything in your strings. Just put them between quotation marks. If the string contains a quotation mark or some other "special character", use a backslash (\) to escape it.Check also [How to escape special characters in building a JSON string?](http://stackoverflow.com/questions/19176024/how-to-escape-special-characters-in-building-a-json-string) – user2314737 Dec 18 '15 at 07:28
  • @user2314737 Thanks! Works! – Chirpizard Dec 18 '15 at 08:05

1 Answers1

2

About the apostrophes and spaces: If you're talking about JavaScript property names (keys), there's no problem having apostrophes or spaces in them, you just use brackets notation and a string:

var obj = {};
obj["Chieftain's Blade"] = 42;
console.log(obj["Chieftain's Blade"]); // 42

Note that the string can come from any expression, not just a literal. So:

var obj = {};
obj["Chieftain's Blade"] = 42;
var name = "Chieftain's Blade";
console.log(obj[name]); // 42

If you want to define a property like that in an object initializer, you just use quotes:

var obj = {
    "Chieftain's Blade": 42
};
console.log(obj["Chieftain's Blade"]); // 42
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Thanks, TJ, I had tried putting quotes around the object property and got an error, but have grabbed the semi-colon in the quotes hah – Chirpizard Dec 18 '15 at 08:04