Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
As we already know, one of the differences between an array and object is:
"If you want to supply specific keys, the only choice is an object. If you don't care about the keys, an array it is" (Read more here)
Besides, according to MDN's documentation:
Arrays cannot use strings as element indexes (as in an associative
array) but must use integers
However, I was surprised that:
> var array = ["hello"]; // Key is numeric index
> array["hi"] = "weird"; // Key is string
content structure looks like: ["hello", hi: "weird"]
The content structure of the array looks weird.
Even more, when I check the type of array it returns true
Array.isArray(array) // true
Questions:
Why have this behavior? This seems inconsistent, right?
What is the data structure actually storing behind the scene: as an array or something like an object, hash table, linked list?
Does this behavior depend on a specific JavaScript engine (V8, spidermonkey, etc..) or not?
Should I use arrays like this (keys are both numeric index and string) over normal objects?
An array in JavaScript is not a separate type, but a subclass of object (an instance of the class Array, in fact). The array (and list) semantics are layered on top of the behavior that you get automatically with every object in JavaScript. JavaScript objects are actually instances of the associative array abstract data type, also known as a dictionary, map, table, etc., and as such can have arbitrarily-named properties.
The rest of the section of the MDN docs you quoted is important:
Setting or accessing via non-integers using bracket notation (or dot
notation) will not set or retrieve an element from the array list
itself, but will set or access a variable associated with that array's
object property collection. The array's object properties and list of
array elements are separate, and the array's traversal and mutation
operations cannot be applied to these named properties.
So sure, you can always set arbitrary properties on an array, because it's an object. When you do this, you might, depending on implementation, get a weird-looking result when you display the array in the console. But if you iterate over the array using the standard mechanisms (the for...of loop or .forEach method), those properties are not included:
> let array = ["hello"]
> array["hi"] = "weird"
> for (const item of array) { console.log(item) }
hello
> array.forEach( (item) => console.log(item) )
hello
The MDN statement that "The array's object properties and list of array elements are separate" is slightly exaggerated; the indexed elements of an array are just ordinary object properties whose keys happen to be numbers. So if you iterate over all of the properties with for..in, then the numeric ones will show up along with the others. But as is well-documented, for..in ignores the Array-ness of arrays and should not be used if you're looking for arraylike behavior. Here's another page from MDN that talks about that:
Array iteration and for...in
Note: for...in should not be used to iterate over an Array where the index order is important.
Array indexes are just enumerable properties with integer names and are otherwise identical to general object properties. There is no
guarantee that for...in will return the indexes in any particular
order. The for...in loop statement will return all enumerable
properties, including those with non–integer names and those that are
inherited.
Because the order of iteration is implementation-dependent, iterating over an array may not visit elements in a consistent order.
Therefore, it is better to use a for loop with a numeric index (or
Array.prototype.forEach() or the for...of loop) when iterating over
arrays where the order of access is important.
So to answer your questions:
Why have this behavior? This seems inconsistent, right?
"Why" questions are always difficult to answer, but fundamentally, the answer in this case seems to be that what you are seeing is not an intentional behavior. As far as we can tell, Brendan Eich and the subsequent designers of JavaScript didn't set out to make an array type that also allows non-numeric keys; instead, they elected not to make an array type at all. In JavaScript, there are only eight types: Null, Undefined, Boolean, Number, BigInt, String, Symbol, and Object. Array didn't make the cut — it's not a separate type, but just a class of object. In fact, you could remove arrays from the core language and implement them entirely in JavaScript itself (although you'd lose the convenience of the [...] literal syntax).
So that's why: JavaScript arrays aren't their own type, but just a class of object; all objects in JavaScript are associative arrays; therefore, you can use arrays as associative arrays.
What is the data structure actually storing behind the scene: as an array or something like object, hashtable, linkedlist?
The ECMAScript specification requires objects to function as associative arrays, but does not dictate the implementation; you can assume that they're hash tables without losing any generality. But since the Array class is typically part of the core language implementation rather than pure JavaScript runtime code, I would not be surprised to find that it included special optimizations beyond the generic-object property-handling code for handling the numerically-indexed values more efficiently. As long as the semantics are the same, it doesn't matter.
Whether this behavior depends on a specific JavaScript Engine (V8, SpiderMonkey, etc..) or not?
Different engines might change how Array values are displayed/serialized, and in particular whether such string representations include the non-numeric properties. But the underlying behavior of being able to store arbitrary key–value pairs is a natural side-effect of the language's design and should be universal across all implementations compliant with the ECMAScript specification.
Should I use array like this(keys are both numeric index and string) over normal object?
Well, again, an array is a normal object. But for maximum readability and minimum surprise to those reading your code, I would not recommend using the same object as both a regular and associative array. You could implement a new class that has array-like behavior, maybe even inherit from the Array prototype, but it's usually going to be better to keep those two data structure types separate.
This stumped me recently as well! It seemed to me that if JavaScript allows something like ['a', b: 'c'], it must support associative arrays, so why all the redundancy?
Upon further research, I realized while this does look like an associative array or a JS object…
It's not an associative array, because JS does not actually support them unless we are synonymizing "associative array" and "object".
It IS an array, which is why Array.isArray(array) returned true.
EPIPHANY moment — arrays ✨ ARE ✨ objects. Hear me out 👇🏼
TL;DR
Here are the TL;DR versions of my answers to your questions. For more context and examples, keep scrolling…
On the surface, it does seem inconsistent, but it's actually very consistent when you take a deeper look at the prototype chain and primitive types vs. reference types.
Your array is an array even behind the scenes, but arrays in JS are a type of object and thus can be assigned custom properties in addition to their usual array elements.
Nope! No specific engine needed for this. This is just pure JavaScript.
If you require keys to be numeric and alphanumeric, use an object. HOWEVER, if you would like to use an array, it's perfectly okay to add custom properties to that array as well. Just keep in mind they are only properties of the object, not array elements.
If you require keys, and you also want to maintain the insertion order of your keys (since JS objects cannot reliably retain their property order), consider using a Map object-type instead.
The long version
Almost everything in JavaScript is some sort of object in one way or another, especially if you start exploring the __proto__ chain (MDN docs). In the same way, your array is an array, but it's also an object, just as all arrays are.
However, when you add a value to your array with a key like array["hi"] = "weird", you're not actually pushing a new value to the array, or even a key to the object for that matter in the usual JS objects kind of way, but rather, you are adding a property to the object that the array actually belongs to. So you will still be able to call that property like this:
array.hi // -> "weird"
…but this item is not actually an element in your array, so the below array, even though it's full of properties still only has a length of one.
const array = [];
array.push(1);
array['a'] = 'funky';
array.length; // -> 1
Even more interesting, this only happens when trying to pass an index with an alpha character in it. Even if you pass a numerical string, it will still push the value to the array in the usual/expected way. It's when alpha characters are present that arrays fall back to assigning properties instead of pushing array items.
Stranger yet — you can not only reference these properties without them "counting against" your array length, but you can actually store entire arrays and objects in these properties too as you would any other JS object.
const array = [];
array.push(1);
array['a'] = { b: 'test1', c: ['test2', 'test3']};
array.a.c[1]; // -> "test3"
array.length; // -> 1
Answers to your questions
1. Why have this behavior? This seems inconsistent, right?
Yes and no. This behavior is actually there to be consistent with the object-oriented nature of JavaScript as a whole. At the same time, as you mentioned, this is not consistent with the usual usage of JavaScript arrays and in most cases would likely baffle another developer working on a project where this was used in practice, so I wouldn't advise assigning properties to an array as a means of entry into that array, as it does not function that way.
However, properties are useful in all objects, and it is perfectly safe to add custom properties to any object as long as you are careful not to overwrite existing ones you might need. When you initialize an array [], it has a length of 0, right? Right. But does it already have inherent custom properties? Yep!
If you run the below code, you get ["length"] as a property name that already exists on an empty array.
Object.getOwnPropertyNames([]); // -> ["length"]
Similarly, if you add your own properties, they will also show up when calling this method, along with your array elements:
let array = ['a','b','c'];
array['test'] = true;
Object.getOwnPropertyNames(array); // -> ["0", "1", "2", "length", "test"]
More practically, you may have a time when you want to add a special function to an array in your project but not necessarily want that function to spread across all other arrays. Rather than implementing this as a new prototype function, you can add this new function to a specific array by adding the function as the value of a property as you did in your example. Your properties can even interact with each other.
let array = ['a','b','c'];
array.doubleLength = function() { return this.length * 2 };
array.hasAtLeast = function(x) { return this.length >= x };
array.length; // -> 3
array.doubleLength(); // -> 6
array.hasAtLeast(1); // -> true
array.hasAtLeast(3); // -> true
array.hasAtLeast(4); // -> false
2. What is the data structure actually storing behind the scene: as an array or something like object, hashtable, LinkedList?
I discussed this in more detail above as well. Essentially, what is happening here is that the new properties are being stored as object properties, but not in the usual object { key: value } key-value pair sort of way. Arrays and objects are both actually different types of what we'll refer to here as "raw objects". Everything in JavaScript is some instance of a raw object in one way or another. Arrays are a type of "raw" object. Key-value pair objects are a type of object and are also not these "raw" objects we are talking about, but another manifestation of them.
All objects shared a few key attributes. One of those attributes is that they all share the same ability to store properties, which we normally recognize as the keys in the objects we work with on a daily basis. All objects allow the addition of custom properties.
Some types of objects like strings require you to traverse to their prototypes in order to add properties. Because of this, this won't work:
let string = "Hello World";
string.addExclamation = function() { return this + "!" };
string.addExclamation() // -> THROWS ERROR!!!
…but this WILL work:
let string = "Hello World";
string.__proto__.addExclamation = function() { return this + "!" };
string.addExclamation(); // -> "Hello World!"
Other types of objects including both key-value objects and arrays, allow you to add custom properties directly to them without traversing to their prototypes, as I mentioned in my answer to your first question above.
This distinction between which types of objects allow the direct adding of properties and which don't, and which we'd consider "raw" objects and which we wouldn't boils down to a JavaScript ideology known as "primitive types" and "reference types". I highly suggest reading up on that "primitive vs. reference" distinction more.
It's also useful to note how to set up these prototype properties and the difference between prototype and __proto__. In many ways, prototype and __proto__ work the same, the key difference being that you can call the __proto__ property on any variable/object to get the prototype for its object type. On the other hand, prototype can be called only on the actual constructor for a variable/object. The below two prototype and __proto__ lines actually call the same object.
const string = "";
string.__proto__; // -> String { … }
String.prototype; // -> String { … }
string.__proto__ === String.prototype; // -> true
3. Whether this behavior depends on a specific Javascript Engine (V8, SpiderMonkey, etc..) or not?
Nope! This is about as raw as it comes when working with JavaScript. Just the beauty of object-oriented programming. I explain this in a lot more depth in the above two answers to your questions, as well as the content I added before my answers.
4. Should I use an array like this (keys are both numeric index and string) over a normal object?
Contrary to what a lot of answers are saying, yes 👏🏼 do use arrays like this IF your situation calls for it. I don't mean to contradict myself here. Earlier, I did say you may want to avoid this, and that's true, so allow me to elaborate.
Should you add elements to arrays like this, where it adds them as properties and not as array elements? No. Adding to arrays like this is actually adding JS properties to them, not array elements.
Is there ever a case where I SHOULD add properties to arrays like this? Yes! It may not be often, and I explain this in more detail in my response to your first question, but if you are in a situation where you want to add custom properties, even functional properties to one specific array and not the Array.prototype which would affect all arrays, this is a perfectly fine and safe way to do so. I'll add my same example from my answer to question #1 below for reference:
let array = ['a','b','c'];
array.doubleLength = function() { return this.length * 2 };
array.hasAtLeast = function(x) { return this.length >= x };
array.length; // -> 3
array.doubleLength(); // -> 6
array.hasAtLeast(1); // -> true
array.hasAtLeast(3); // -> true
array.hasAtLeast(4); // -> false
For further reading, I would highly encourage reading my post in its entirety and chewing on it, and then checking out the MDN docs on the prototype chain. For even further study, I highly suggest signing up for Just JavaScript, a completely free 10-part email digest series and takes you on a deep dive into JavaScript Mental Models, which focuses largely on this prototype chain and other fascinating parts of what makes JavaScript so robust. It's written by Dan Abramov, one of the co-creators of Redux, and beautifully illustrated by Maggie Appleton. Definitely worth a peek!
Other answers look good.
In JavaScript, an array is an object.
For example, you can destructure an array as an object due to each item in an array is an object with key-value pairs.
const array = ["Hello", "World"];
const {0: first, 1 :second} = array;
console.log({first, second});
And in case that the keys of each item in an array including integer, string, etc. You can object destructure by a specific key.
var array = ["hello"];
array["hi"] = "weird";
const {
0: first, // integer --> index number
hi: second // string --> string key
} = array;
console.log({first, second});
Besides, you can only access an item by index when key is an integer.
var array = ["hello"];
array["hi"] = "weird";
console.log(array[0]); // hello
console.log(array[1]); // undefined
console.log(array["hi"]); // weird
Why does array allow string as an index in JavaScript
To add custom behaviour to an object
Question 1.1
Why have this behavior?
This is because array is itself an object with specific behaviour.
Array is a data structure which defines the behaviour of the object. So when we declare array we tell the compiler that this object must be treated as an array so the compiler only considers numeric indexes in an array.
For an array:
let array = []
If you write like this:
array["hi"] = "weird";
The compiler treats it as a property of array and in array, all properties are ignored by default
length is one of the properties of an array which returns it's length.
Since we know that length is a property of array. Try running the below program you'll get the idea.
let array = [];
array[0] = "first";
array[1] = "second";
array[2] = "third";
array[3] = "fourth";
array["hi"] = "weird";
// we overrided a predefined property of an array. You can try changing it's length and see what happens
array["length"] = 3;
console.log(array, '\n Length of array: ', array.length);
Question 1.2
This seems inconsistent, right?
No, what if we want to add custom behaviour to the array. However, it can be done usnig 'prototype` but it applies for all the arrays but for specific array it might be needed. However, it is never needed.
Question 2
What is the data structure actually storing behind the scene: as an array or something like object, hashtable, linkedlist?
It is stored as an object since it is a data structure
Question 3
Whether this behavior depends on a specific Javascript Engine (V8, SpiderMonkey, etc..) or not?
Behaviour of this won't change for javascript operations but handling of operations may differ.
If you run the above snippet in Console of Dev Tools in Chrome, the hi index is printed whereas it doesn't in the case of the Firefox and stackoverflow because they use different engines
You can read more about it here
Question 4
Should I use array like this(keys are both numeric index and string) over normal object?
No, it doesn't make sense to use array to store values in a (string)key when you cannot use the string keys in methods like forEach, map and array related methods. It'll make things complicated but still if you want to do it then use an object instead.
You can read more about it here
Answer to question 1 and 2
Arrays are so-called "Integer Indexed Exotic Objects" in ECMAScript which are based on which in return belong to the object data type. Exotic objects are objects with some special behavior such as integer indices in our case. As far as I can tell, since we set a string index, our array is no longer an integer indexed exotic object.
Answer to question 3
Since this is ECMAScript, my understanding is, that all JavaScript engines have to implement it this way to stay compatible with the specification.
Answer to question 4
You should definitely not. Arrays should only have integer indices as per spec ("Integer Indexed Exotic Objects"). If you want to use string or mix both, use an object. When using an object, integer indices are coerced to string indices.
Note that this will only work in Chrome or in any other V8 engine browser. I tested your code in Safari and it didn't store the "hi" key-value pair at all. But in Safari it is rather an exception than the rule since you don't get an error either.
The reason why it works is that it is still an object. The functionalities of arrays are stacked on top of what objects in general have.
typeof array // "object"
Edit
By the advice of #Kevin B, I realized that Safari indeed doesn't handle it differently than Chrome. The console.log implementation must be different in both browsers in this regard. I was still able to access the key value pair even if it wasn't shown when printing the array in the console.
Also I noticed that the OP is looking for an answer from a reputable source. The only reputable source that I came across which exactly reflects what I wrote above is from w3schools.com
Arrays are Objects
Arrays are a special type of objects. The typeof operator in
JavaScript returns "object" for arrays.
But, JavaScript arrays are best described as arrays.
Arrays use numbers to access its "elements". In this example,
person[0] returns John:
[...]
In JavaScript, there are two categories of data types: primitive types (number, string, boolean, bigInt, symbol, undefined, null) and objects (also known as Reference types).
An array is a special object that uses integers as its keys. JavaScript still treats it as an object. That means you can add/remove properties (That array will be called associative array or dictionary, like your example). If you want to truly treat it as a standard array, you should never use strings as keys.
Furthermore, you should use arrays as standard arrays because in JavaScript arrays have behavior and optimizations specific to their intended use. Also, when you add a new property with a string key to your array, the value of length will not change.
var array = ["hello"]; // Another way to declare a array is array = new Array("Hello"). This version clearly shows array is just a object.
array["hi"] = "weird";
console.log(array.length) // 1 not 2
array[1] = "A.Phong"
console.log(array.length) // 2
Attending to it's specification, JSON elements (and javascript objects) are unordered so, even in almost all cases, when you iterate over a javascript object, you get elements in the same order they was defined; you definitively cannot trust in that order because engine is allowed to alter it.
This is extremely rare. I have been able to observe it one time, but I don't find that code right now and I don't remember the exact version of JS engine (but it was node). If I manage to find it, I will add it to this post.
That being said, the point is that code relying in this behaviour can (and should) be considered buggy because, even it will work as expected in most engines, it may fail typically because of internal engine optimisations.
For example:
"use strict";
var x = {
b: 23,
a: 7
};
function someSorting(input) {
var output = {};
Object.keys(input).sort().map(
function(k){
output[k] = input[k];
}
);
return output;
};
x = someSorting(x);
// Some smart engine could notice that input and output objects have the
// exact same properties and guess that, attending the unordered nature of
// javascript Object, there is no need to actually execute someSorting();
console.log(x);
// Usually will display: { a: 7, b: 23 }
// But perfectly we could got: { b: 23, a: 7 }
I know there is too many literature (even StackOverflow questions) about this (NON-) issue and "workarrounds" to achieve the expected behaviour by sorting keys in a separate array.
But doing so code goes too messy compared in simply trusting in key order.
I'm pretty sure that this can be achieved in a more elegant fashion by implementing a so-called "sObject" alternative having native Object as its prototype but overloading it's native iterator and setter so that:
When any new property is added, it's key is appended to an Array index mantained under the hood.
When an sObject instance is iterated, our customized iterator uses that index to retrieve elements in the right order.
In summary: Actual Object specification is right because, in most cases, properties order doesn't care. So I think that engine optimisations that could mess it are wonderfull.
But it would be also wonderful to have an alternative sObject with which we could do something like:
var x = new sObject({b: 23, a: 7});
...and trust that we could iterate it in the same exact order or, also / at least, do some sorting task over it and trust that this will not be altered.
Of course!! I'm initalyzing it with a native javascript Object so, in fact, theoretically we can't trust that it will be populated right (even I can't imagine why any engine optimisation should alter it before any operation).
I used that notation for brevity (and, I confess) because I expect that, in that case should work always (even I'm not really sure). However we even could sort it later (which, in most cases we will do that way) or use other kind of initialization like providing a JSON string or an array of objects (or arrays) with single key and value pairs.
My concern is: Such a thing exists yet? I wasn't able to find it. But sure I'm not the first guy thinking in that...
I can try to implement it (I'm thinking about that). I think it's possible and that I could achieve it. But it's not as simple so first I want to be sure that I'm not reinventing the wheel...
So any comments, suggestions, etc... will be welcome.
Sure, you could do all this. You will need some machinery such as Object.observer, which is currently only available in Chrome. We can define this as the following:
function myObject(object) {
// if the object already has keys, bring them in in whatever order.
var keys = Object.keys(object);
// Override Object.keys to return our list of keys.
Object.defineProperty(object, 'keys', { get: function() { return keys; });
// Watch the object for new or deleted properties.
// Add new ones at the end, to preserve order.
Object.observe(object, function(changes) {
changes.forEach(function(change) {
if (change.type === 'add') keys.push(change.name);
if (change.type === 'delete') keys = keys.filter(function(key) {
return key === change.name;
});
});
});
return object;
}
Note that Object.observe is asynchronous, so even after you add a property to the object, it won't be reflected in the custom keys property until after a tick of the clock, although in theory you could use Object.deliverChangedRecords.
The above approach uses a function which adds the new ordered key functionality to an existing object. Of course there are other ways to design this.
This "solution" obviously cannot control the behavior of for...in loops.
(Let us suppose that there is a good reason for wishing this. See the end of the question if you want to read the good reason.)
I would like to obtain the same result as a for in loop, but without using that language construct. By result I mean only an array of the property names (I don't need to reproduce the behavior that would happen if I modify the object while iterating over it).
To put the question into code, I'd like to implement this function without for in:
function getPropertiesOf(obj) {
var props = [];
for (var prop in obj)
props.push(prop);
return props;
}
From my understanding of the ECMAScript 5.1 specification about the for in statement and the Object.keys method, it seems the following implementation should be correct:
function getPropertiesOf(obj) {
var props = [];
var alreadySeen = {};
// Handle primitive types
if (obj === null || obj === undefined)
return props;
obj = Object(obj);
// For each object in the prototype chain:
while (obj !== null) {
// Add own enumerable properties that have not been seen yet
var enumProps = Object.keys(obj);
for (var i = 0; i < enumProps.length; i++) {
var prop = enumProps[i];
if (!alreadySeen[prop])
props.push(prop);
}
// Add all own properties (including non-enumerable ones)
// in the alreadySeen set.
var allProps = Object.getOwnPropertyNames(obj);
for (var i = 0; i < allProps.length; i++)
alreadySeen[allProps[i]] = true;
// Continue with the object's prototype
obj = Object.getPrototypeOf(obj);
}
return props;
}
The idea is to walk explicitly the prototype chain, and use Object.keys to get the own properties in each object of the chain. We exclude property names already seen in previous objects in the chain, including when they were seen as non-enumerable. This method should even respect the additional guarantee mentioned on MDN:
The Object.keys() method returns an array of a given object's own
enumerable properties, in the same order as that provided by a
for...in loop [...].
(emphasis is mine)
I played a bit with this implementation, and I haven't been able to break it.
So the question:
Is my analysis correct? Or am I overlooking a detail of the spec that would make this implementation incorrect?
Do you know another way to do this, that would match the implementation's specific order of for in in all cases?
Remarks:
I don't care about ECMAScript < 5.1.
I don't care about performance (it can be disastrous).
Edit: to satisfy #lexicore's curiosity (but not really part of the question), the good reason is the following. I develop a compiler to JavaScript (from Scala), and the for in language construct is not part of the things I want to support directly in the intermediate representation of my compiler. Instead, I have a "built-in" function getPropertiesOf which is basically what I show as first example. I'm trying to get rid of as many builtins as possible by replacing them by "user-space" implementations (written in Scala). For performance, I still have an optimizer that sometimes "intrinsifies" some methods, and in this case it would intrinsify getPropertiesOf with the efficient first implementation. But to make the intermediate representation sound, and work when the optimizer is disabled, I need a true implementation of the feature, no matter the performance cost, as long as it's correct. And in this case I cannot use for in, since my IR cannot represent that construct (but I can call arbitrary JavaScript functions on any objects, e.g., Object.keys).
From the specification point of view, your analysis correct only under assumption that a particular implementation defines a specific order of enumeration for the for-in statement:
If an implementation defines a specific order of enumeration for the
for-in statement, that same enumeration order must be used in step 5
of this algorithm.
See the last sentence here.
So if an implementation does not provide such specific order, then for-in and Object.keys may return different things. Well, in this case even two different for-ins may return different things.
Quite interesting, the whole story reduces to the question if two for-ins will give the same results if the object was not changed. Because, if it is not the case, then how could you test "the same" anyway?
In practice, this will most probably be true, but I could also easily imagine that an object could rebuild its internal structure dynamically, between for-in calls. For instance, if certain property is accessed very often, the implementation may restructure the hash table so that access to that property is more efficient. As far as I can see, the specification does not prohibit that. And it is also not-so-unreasonable.
So the answer to your question is: no, there is no guarantee according to the specification, but still will probably work in practice.
Update
I think there's another problem. Where is it defined, what the order of properties between the members of the prototype chain is? You may get the "own" properties in the right order, but are they merged exactly the way as you do it? For instance, why child properties first and parent's next?
a = {b:'bb',c:'cc',d:'dd'};
$.each(a, function(index){
// here 'index' is a string variable containing the property name, ie 'b', 'c' or 'd'.
}
I want the integer index of the property - is this possible without using an independent count variable?
You could do this:
Object.keys( a ).forEach(function ( name, index ) {
var value = a[name];
name // the property name
value // the value of that property
index // the counter
});
This is ES5 API, so you'll need es5-shim for IE8.
No, it's an object. When you add a new property to a JS object what position is that property supposed to be at? What if you delete a property in the middle and then add it again? What if you merge another object over the original when the merging object had like-named properties in a completely different order in its source code?
You could rig a count and it might work in a lot of cases but there's no guarantee you'd get things in the order you might expect. When order matters, you use an array. When it doesn't and you want to conveniently access things by name you use an object literal. Or you wrap an array in an object that maintains a map to array indeces when they're sorted by writing array sort methods that also sort the key/name map array.
Also, learn for/in loops. There's no reason to use jQuery to iterate a non-jQuery object. At best it will just be marginally slower.
Edit:
Well if you want a count baked in and not an index you can rely on for future reference then I'd just bake my own object type if I had to deal with IE8 since we generally try not to touch the Object constructor prototype (although for normalization to future spec I guess it's might be okay if you can do it without breaking things like for/in loops used on arrays which normally wouldn't iterate the prototype methods).
function IterableCounterObj(objLiteral){ //or perhaps something less painfully named
this.each = function(iteratorFunc){
var cnt = 0;
for(var x in objLiteral){
cnt++;
iteratorFunc(x,objLiteral[x],cnt);
}
}
}
var a = new IterableCounterObj({b:'bb',c:'cc',d:'dd'});
a.each(function(name,value,count){ console.log(count); });
//too much? Here's the jQuery approach to cruft
var $i = function(objLiteral){
return new IterableCounterObj(objLiteral);
}
$i({b:'bb',c:'cc',d:'dd'}).each(function(name,value,count){ console.log(count); });
I read a few questions and answers about javascript dictionary implementations, but they don't meet my requirements:
the dictionary must be able to take objects as keys
the values must be accessible by the []-operator
So I came up with the idea to overwrite the valueOf-method in Object.prototype, as follows:
Object.__id__ = 0;
Object.prototype.valueOf = function() {
if(!this.__id__)
this.__id__ = ++Object.__id__;
return "__id__" + this.__id__;
}
Object.prototype.toString = Object.prototype.valueOf;
//test
var x = {p1: "5"};
var y = [6];
var z = {};
z[x] = "7";
z[y] = "8";
console.log(z[x], z[y]);
I tested this with google-chrome and it seems to work well, but I'm a bit sceptical, whether this will cause some drawbacks, since it was so easy to implement.
Considering that the valueOf method is not used for other purposes in the whole code, do you think there are any disadvantages?
It's an interesting idea. I suggest my jshashtable. It meets your first requirement but not the second. I don't really see the advantage of insisting on using the square bracket property access notation: do you have a particular requirement for it?
With jshashtable, you can provide a hashing function to the Hashtable constructor. This function is passed an object to be used as a key and must return a string; you could use a function not dissimilar to what you have there, without having to touch Object.prototype.
There are some disadvantages to your idea:
Your valueOf method will show up in a for...in loop over any native object;
You have no way determining which keys should be considered equal, which is something you may want to do. Instead, all keys will be considered unique.
This won't work with host objects (i.e. objects provided by the environment, such as DOM elements)
It is an interesting question, because I had so far assumed that any object can be used as an index (but never tried with associative arrays). I don't know enough about the inner workings of JavaScript to be sure, but I'd bet that valueOf is used somewhere else by JavaScript, even if not in your code. You might run into seemingly inexplicable problems later. At least, I'd restrict myself to a new class and leave Object alone ;) Or, you explicitly call your hashing function, calling it myHash() or whatever and calling z[x.myHash()] which adds clutter but would let me, personally, sleep better ;) I can't resist thinking there's a more JavaScript-aware solution to this, so consider all of these ugly workarounds ;)
If you came upon this question looking for a JS dictionary where objects are keys look at Map Map vs Object in JavaScript