javascript array associative AND indexed?

2019-01-04 11:22发布

can an array in JS be associative AND indexed? I'd like to be able to lookup an item in the array by its position or a key value.. possible?

10条回答
SAY GOODBYE
2楼-- · 2019-01-04 11:50
var stuff = [];
stuff[0] = "foo";
stuff.bar = stuff[0]; // stuff.bar can be stuff["bar"] if you prefer
var key = "bar";
alert(stuff[0] + ", " + stuff[key]); // shows "foo, foo"
查看更多
贪生不怕死
3楼-- · 2019-01-04 11:50

Yes.

test = new Array();
test[0] = 'yellow';
test['banana'] = 0;
alert(test[test['banana']]);
查看更多
Luminary・发光体
4楼-- · 2019-01-04 11:53

After reading the Wikipedia definition of associative array, I'm going to break with traditional JavaScript lore and say, "yes, JavaScript does have associative arrays." With JavaScript arrays, you can add, reassign, remove, and lookup values by their keys (and the keys can be quoted strings), which is what Wikipedia says associative arrays should be able to do.

However, you seem to be asking something different--whether you can look up the same value by either index or key. That's not a requirement of associative arrays (see the Wikipedia article.) Associative arrays don't have to give you the ability to get a value by index.

JavaScript arrays are very closely akin to JavaScript objects.

  arr=[];
  arr[0]="zero";
  arr[1]="one";
  arr[2]="two";
  arr["fancy"]="what?";

Yes, that's an array, and yes, you can get away with non-numeric indices. (If you're curious, after all this, arr.length is 3.)

In most cases, I think you should stick to numeric indices when you use arrays. That what most programmers expect, I think.

The link is to my blog post about the subject.

查看更多
不美不萌又怎样
5楼-- · 2019-01-04 11:57
var myArray = Array();
myArray["first"] = "Object1";
myArray["second"] = "Object2";
myArray["third"] = "Object3";

Object.keys(myArray);              // returns ["first", "second", "third"]
Object.keys(myArray).length;       // returns 3

if you want the first element then you can use it like so:

myArray[Object.keys(myArray)[0]];  // returns "Object1"
查看更多
Bombasti
6楼-- · 2019-01-04 12:00

Although I agree with the answers given you can actually accomplish what you are saying with getters and setters. For example:

var a = [1];
//This makes a["blah"] refer to a[0]
a.__defineGetter__("blah", function(){return this[0]});
//This makes a["blah"] = 5 actually store 5 into a[0]
a.__defineSetter__("blah", function(val){ this[0] = val});

alert(a["blah"]); // emits 1
a["blah"] = 5;
alert(a[0]); // emits 5

Is this what you are looking for? i think theres a different more modern way to do getters and setters but cant remember.

查看更多
戒情不戒烟
7楼-- · 2019-01-04 12:06

I came here to wanting to know if this is bad practice or not, and instead found a lot of people appearing not to understand the question.

I wanted to have a data structure that was ordered but could be indexed by key, so that it wouldn't require iteration for every lookup.

In practical terms this is quite simple, but I still haven't read anything on whether it's a terrible practice or not.

var roygbiv = [];
var colour = { key : "red", hex : "#FF0000" };
roygbiv.push(colour);
roygbiv[colour.key] = colour;
...
console.log("Hex colours of the rainbow in order:");
for (var i = 0; i < roygbiv.length; i++) {
    console.log(roygbiv[i].key + " is " + roygbiv[i].hex);
}

// input = "red";
console.log("Hex code of input colour:");
console.log(roygbiv[input].hex);

The important thing is to never change the value of array[index] or array[key] directly once the object is set up or the values will no longer match. If the array contains objects you can change the properties of those objects and you will be able to access the changed properties by either method.

查看更多
登录 后发表回答