views:

32

answers:

3

hello,
i.g. we have next hash

ahash = {"one": [1,2,3], "two": [4,5,6]}

is there any function, which returns me value of the first key in hash? (i don't know key name, i just want to get value of first key). thanks

+2  A: 

There's no such thing as the "first" key in a hash (Javascript calls them objects). They are fundamentally unordered. Do you mean just choose any single key:

for (var k in ahash) {
    break
}

// k is a key in ahash.
Ned Batchelder
+1  A: 

Try this:

for (var firstKey in ahash) break;

alert(firstKey);  // 'one'
nickf
A: 

you can put your elements into an array and hash at the same time.

var value = [1,2,3];
ahash = {"one": value};
array.push(value);

array can be used to get values by their order and hash could be used to get values by their key. just be be carryfull when you remove and add elements.

yilmazhuseyin