Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Case Suggestion: How to access Values in objects in JavaScript #130

Closed
politologu opened this issue Apr 2, 2018 · 1 comment
Closed
Labels

Comments

@politologu
Copy link
Contributor

politologu commented Apr 2, 2018

const car = {
wheels: 4,
name: "minicooper",
color: "pink"
};
car.name;
// "minicooper"

@LeusMaximus
Copy link

Don't forget about "square bracket notation"
The dot requires the key to be a valid variable identifier. That is: no spaces and other limitations.
There’s an alternative “square bracket notation” that works with any string:

const car = {
  wheels: 4,
  name: "minicooper",
  color: "pink",
  'Is this a good car?': true
};

car['Is this a good car?']; // true

With the brackets notation we can obtain the property name as the result of any expression:

const key = 'name';
const type = 'car';

car[key]; // minicooper
car['Is this a good ' + type + '?']; // true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants