Access non-numeric Object properties by index?
No, it isn't possible."I'm specifically looking to target the index, just like the first example - if it's 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 IndexSample 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()
:
Then take the key from first index.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.
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:And identifiers may not start with a digit as described here:MemberExpression
.
IdentifierName
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.
- IdentifierName ::
- IdentifierStart
IdentifierName IdentifierPart
- IdentifierStart ::
- UnicodeLetter
$
_
\
UnicodeEscapeSequence
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
How to Bind Bootstrap Popover on Dynamic Elements
JavaScript Add Method to Object
Does Use of Anonymous Functions Affect Performance
Difference Between Obtrusive and Unobtrusive JavaScript
How to Read the Correct Time/Duration Values from Google Spreadsheet
How to Pass a Flag to Gulp to Have It Run Tasks in Different Ways
Is Localstorage.Getitem('Item') Better Than Localstorage.Item or Localstorage['Item']
How to Check If an Array Is a Subset of Another Array in JavaScript
Why Does Console.Log Say Undefined, and Then the Correct Value
Get the Current Year in JavaScript
How to Get Month and Date of JavaScript in 2 Digit Format
Angular 2 Karma Test 'Component-Name' Is Not a Known Element
JSON Transfer of Bigint: 12000000000002539 Is Converted to 12000000000002540
Options for Testing Service Workers via Http
Do Browsers Parse JavaScript on Every Page Load
Professional Jquery Based Combobox Control
How to Set the Style -Webkit-Transform Dynamically Using JavaScript