How to Get a JavaScript Object Property Name That Starts with a Number

Can I get a javascript object property name that starts with a number?

You can use the following syntax to do what you describe using bracket notation:

myObject["myProperty"]

Bracket notation differs from dot notation (e.g. myObject.myProperty) in that it can be used to access properties whose names are illegal. Illegal meaning that with dot notation, you're limited to using property names that are alphanumeric (plus the underscore _ and dollar sign $), and don't begin with a number. Bracket notation allows us to use a string to access a property and bypass this.

myObject.1 // fails, properties cannot begin with numbers
myObject.& // fails, properties must be alphanumeric (or $ or _)

myObject["1"] // succeeds
myObject["&"] // succeeds

This also means we can use string variables to look up and set properties on objects:

var myEdgyPropertyName = "||~~(_o__o_)~~||";

myEdgyObject[myEdgyPropertyName] = "who's there?";

myEdgyObject[myEdgyPropertyName] // "who's there?";

You can read more about dot and bracket notation here, on MDN.

Object property name as number

You can reference the object's properties as you would an array and use either me[123] or me["123"]

Getting object properties that start with a number

You can use bracket access for properties that aren't valid JavaScript identifiers, that goes for property names with spaces or other language symbols like +, *

window.alert(stats.LOAD_AVG["1_MIN"]);

You can use bracket access anywhere really

window.alert(stats["COUNTS"]["TOTAL"]);

Getting the object's property name

Use Object.keys():

var myObject = { a: 'c', b: 'a', c: 'b' };var keyNames = Object.keys(myObject);console.log(keyNames); // Outputs ["a","b","c"]

Javascript: access an object property whose name starts with a number

Use square brackets:

var usedStorage = result.accounts["4Sync"].usedStorage;

Property identifers can begin with a number, but member expressions with the . character will only allow valid variable identifiers (since anything else is ambiguous). To get around this, you can use the square bracket syntax, which is equivalent but allows the use of any string.

If you're interested, here is the grammar:

MemberExpression :

    PrimaryExpression
    FunctionExpression
    MemberExpression [ Expression ]
    MemberExpression . IdentifierName

Notice how square brackets can contain any expression, but the . can only be followed by an IdentifierName (basically, any valid identifier, plus reserved words in ES5).

How to get the property name of object and use that property name in javascript

The only error in your code is mapped_errors.{obj_key}.msg. You should be using [] to access the object's property value when you are using a variable to get the key name, as mapped_errors[obj_key].

var mapped_errors = { _id:   { location: 'body',     param: '_id',     value: 123,     msg: '_id is required and must be a string' } };
let obj_key = Object.keys(mapped_errors);console.log(mapped_errors[obj_key].msg);
mapped_errors = { _name: { location: 'body', param: '_name', value: 123, msg: '_name is required and must be a string' } } obj_key = Object.keys(mapped_errors);console.log(mapped_errors[obj_key].msg);

How to check if a JavaScript Object has a property name that starts with a specific string?

You can check it against the Object's keys using Array.some which returns a bool.

if(Object.keys(obj).some(function(k){ return ~k.indexOf("addr") })){
// it has addr property
}

You could also use Array.filter and check it's length. But Array.some is more apt here.

Javascript Object - Key beginning with number, allowed?

You can use any key if you use [string] to access the key, even key with space. All of these are valid:

myObj['key with space']
myObj['12345']

But if you want to use dot . operator to access the key then the key must be a valid identifier which means they can not begin with a number or contain space.

Get object property name as a string

Yes you can, with a little change.

function propName(prop, value){
for(var i in prop) {
if (prop[i] == value){
return i;
}
}
return false;
}

Now you can get the value like so:

 var pn = propName(person,person.first_name);
// pn = "first_name";

Note I am not sure what it can be used for.

Other Note wont work very well with nested objects. but then again, see the first note.

get an object's property name using its value

The following simple implementation will log the keys for apple in the console.

const alpha = {
a: 'apple',
b: 'bubble'
}

Object.entries(alpha).map(
([key, value]) => {
if (value === 'apple'){
console.log(key);
}
}
)


Related Topics



Leave a reply



Submit