How to access a property of a Javascript object using a text string that is the name of…
- User
How to access a property of a Javascript object using a text string that is the name of the property ?
- Assistant
To access a property of a JavaScript object using a text string that represents the name of the property, you can use the bracket notation. The syntax is as follows:
objectName["propertyName"]Here,
objectNameis the name of your object, and"propertyName"is the string that represents the name of the property you want to access. Remember to enclose the property name within quotes (either single'or double"quotes work).Example:
let person = { name: "John Doe", age: 30, occupation: "Web Developer" }; let propertyName = "age"; console.log(person[propertyName]); // This will log 30 to the console.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.