How to Access a Numeric Property

How to access a numeric property?

This should work:

myObject["0"]

(myObject["propertyName"] is an alternative syntax for myObject.propertyName.)

You're getting the error because, in JavaScript, identifiers can't begin with a numeral. From the Variables page at the Mozilla Developer Centre:

A JavaScript identifier must start
with a letter, underscore (_), or
dollar sign ($); subsequent characters
can also be digits (0-9). Because
JavaScript is case sensitive, letters
include the characters "A" through "Z"
(uppercase) and the characters "a"
through "z" (lowercase).

Access a numeric property from JSON

You should use data[i]['24h_volume_usd'].

Access numeric properties of an object using dot notation

Your question seems to be about why we can’t access array and array-like elements using the dot notation like this:

const v = a.0;

It’s described in the ECMAScript specification:

The dot notation is explained by the following syntactic conversion:

MemberExpression . IdentifierName

And identifiers may not start with a digit as described here:


IdentifierName ::

IdentifierStart

IdentifierName IdentifierPart

IdentifierStart ::

UnicodeLetter

$

_

\ UnicodeEscapeSequence


As for the reasoning, having identifier names just being made of digits would have made it difficult to write number literals. An exception could probably have been designed just for array access but that would have made the language more complex and departing from the common C family syntax without any real gain.

How to access an object's numeric property

I'm not really interested in accessing the values rather than finding
out how they are kept in the class if they're not properties and not
indexed values.

That means diving into PHP´s internals, which is probaly not where you want to find yourself. You'd have to check PHP's own source code to see what exactly is happening in these cases.

Is there a specific reason you want to know?

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"]

how to access javaScript Number type property

Just use the access via []:

alert( person[ 5 ] );

A JavaScript object's properties can be accessed by using . or []. The latter is especially useful for numeric keys or in cases, when you have a key identifier stored in another variable.

Accessing a numeric property in a SimpleXMLElement

$x = 0;
echo $object->$x;

or

echo $object->{0};

The reason is that '0' is not a valid identifier in PHP. So when you type '0', all it sees is a T_LNUMBER. All names follow the varaible naming convention. The only deviation is that a member variable preceded by a -> does not need the $ prefix. http://www.php.net/manual/en/language.variables.basics.php

{0} works, because {} indicates that the identifier is the result of the simple expression inside. So {$x} is the same as $x in this case, but {0} is not the same as '0', since they result in different parser tokens.



Related Topics



Leave a reply



Submit