Access Non-Numeric Object Properties by Index

Access non-numeric Object properties by index?

"I'm specifically looking to target the index, just like the first example - if it's possible."

No, it isn't possible.

The closest you can get is to get an Array of the object's keys, and use that:

var keys = Object.keys( obj );

...but there's no guarantee that the keys will be returned in the order you defined. So it could end up looking like:

keys[ 0 ];  // 'evenmore'
keys[ 1 ]; // 'something'

Is there a way to access an object property using the index?

Getting Object Properties by Index

Sample example that make you understand to achieve what you want:

o = { "key1": "value1", "key2": "value2"};
var idx = 1; // key2

var key = Object.keys(o)[idx];
value = o[key]

console.log(key,value); // key2 value2

javascript's object: Get only the first key of an object

You can use Object.keys():

The Object.keys() method returns an array of a given object's own property names, in the same order as we get with a normal loop.

Then take the key from first index.

var p = {    "p1": "value1",    "p2": "value2",    "p3": "value3"};var objectKey = Object.keys(p)[0];

console.log("the first key is: " + objectKey)

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.

Accessing Object Property Values Within an Array - JavaScript

You can acces items in arrays at given position by their index. In javascript indexes of arrays are starting with 0: myArray[0]. To access the property of the returned object just use dot-notation: myArray[0].myProperty.

let object1 = [{name: "HappyHands31"}, {job: "website developer"}, {city: "Chicago"}];
console.log(object1[1].job);

Get nth element of a key in an object

You can try this,

 nodes = {   "hello.com": {     id: "hello1.com",     id2: "hello2.com",     id3: "hello3.com"   } } console.log(nodes["hello.com"]["id3"]);

Why for..of does not see non-numeric array indexes?

When you use what you are describing as "non-numeric" indexes with an Array, you are mistaken as to what you are doing.

let arr = [12];     // Set a variable equal to an array with only the number 12 in it
arr["fruit"] = "apple"; // Add a new custom property to your instance of an Array
arr["off"] = false // Add a new custom property to your instance of an Array

console.log(arr[0]); // 12
// Notice how the next 3 lines access properties of the Array, not indexed values?
console.log(arr.fruit); // "apple"
console.log(arr.off); // false
console.log(arr.length) // There is only 1 array item, but 2 additional properties

How can JavaScript arrays have non-numeric keys?

With a for..of loop, the object's Symbol.iterator property is called. In the case of an array, this is equivalent to the array's .values() method, which contains the values for each index in the array. Non-numeric properties are not included - arrays generally don't have arbitrary non-numeric properties, and code that does assign arbitrary non-numeric properties to an array is likely in need of refactoring.

A for..in loop iterates over all enumerable properties on an object, including those inherited from the prototype. Thus, for..of on an array will exclude non-numeric properties on an array that a for..in loop will include.

Arrays, being objects, can have arbitrary properties assigned to them, for the most part, just like properties can be assigned to ordinary functions - it's just not a very good idea.



Related Topics



Leave a reply



Submit