0

If I have an object, with properties like name, phone_number, etc...how can I access those in Javascript with a variable?

I want to access the property name in javascript by doing something like this:

object {name : "bob", phone_number : "911" }
propertiesArray = ["name","phone_number"];

 object.propertiesArray[0]; // instead of  object.name;
2

3 Answers 3

2
object[propertiesArray[0]]

This will do the trick. Object attributes can be accessed like array indexes using []

Sign up to request clarification or add additional context in comments.

Comments

0

You can just use object[propertiesArray[0]].

Comments

0

Try

object[propertiesArray[0]];

Comments

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.