2

My Data: I have a an Array of Objects:

var totalsArray = [
  { Applications: 1, Computing Servers: 0, Contracts: 0, Country: "United States" },
  { Applications: 1, Computing Servers: 0, Contracts: 0, Country: "Italy" },
  { Applications: 1, Computing Servers: 0, Contracts: 0, Country: "United States" },
  { Applications: 1, Computing Servers: 0, Contracts: 0, Country: "Spain" }
];

My Code: I then try to extract the keys using the following code:

var objectKeys = d3.keys(totalsArray[0]).filter(function(key) { return key;} );

However, I get an error because of the space in the key named "Computing Servers".

My question: Is there a way to properly create and keep the "space" in the key name/string?

Thank you for any assistance you can offer.

3
  • 2
    use["array notation"] Commented Jan 13, 2014 at 21:43
  • you need to stringify Commented Jan 13, 2014 at 21:44
  • IIRC: you will also need to put quotes around the property names as you are initializing the object. Commented Jan 13, 2014 at 21:44

1 Answer 1

10

Yes, a propery name can be any string (all values are coerced to strings before they are used internally).

This can be done using quotes (double or single) around names in literals:

{ Applications: 1, "Computing Servers": 0, .. },

Just as it can be done with using the obj[prop] syntax (where prop is any expression that can be converted to a sensible string value):

obj["Computing Servers"] = 42;
Sign up to request clarification or add additional context in comments.

1 Comment

This technique is also useful for reserved keywords like "if" or "function" or "continue".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.