How to Access Object Property with Invalid Characters

How to access object property with invalid characters

In JavaScript, object properties can be accessed by dot notation or bracket notation. Dot notation is often cleaner, but has restrictions. As you have noticed, your property contains an invalid character and therefore can't be accessed via dot notation. The solution, then, is to access the property using bracket notation like this: total['ga:newVisits'] so that your complete code will be {{total['ga:newVisits']}}. Live demo here (click).

Another nice feature about bracket notation is that it allows you to use a variable name as a property:

var myObj {
bar: '123'
};
var foo = 'bar';

console.log(myObj[foo]); //logs '123'

How to access object propety when it has special characters in PHP

You need add the property into "{}".

$b2cUser->{'@odata.nextLink'}

How to access object properties with names with special characters? (like quotes)

Are you sure something else isn't going on with your object? Your first example listed worked for me. See screenshot of an object I made with an imported CSV below -

Sample Image

How to access property of javascript object with special character in property name

AngularJS does nothing special here. Refer to the regular rules for referencing properties in a javascript object.

In a controller: console.log($scope.data['Handedness;'])

In a view: {{ data['Handedness;'] }}

How do I access object attributes that contain special characters?

This should work: const price = data['Global Quote']['05. price']

How to destructure object properties with key names that are invalid variable names?

const data = {   "key-with-dash": ["BAZ"]}
const {"key-with-dash": foo} = data;
console.log("foo", foo);


Related Topics



Leave a reply



Submit