From: laredotornado on
Hi,

Given an associative array, how do I get an array of the keys of that
associative array?

Thanks, - Dave
From: David Mark on
laredotornado wrote:
> Hi,
>
> Given an associative array, how do I get an array of the keys of that
> associative array?
>

There's no such thing as an associative array in JS.

If you mean an Object object, use a for-in loop to populate an array.
And make sure you filter the loop with either hasOwnProperty or a
similar wrapper (Google "for-in intrigue").
From: Thomas 'PointedEars' Lahn on
laredotornado wrote:

> Given an associative array, how do I get an array of the keys of that
> associative array?

Mu.

<http://jibbering.com/faq/#posting>


PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$8300dec7(a)news.demon.co.uk>
From: Lasse Reichstein Nielsen on
laredotornado <laredotornado(a)zipmail.com> writes:

> Given an associative array, how do I get an array of the keys of that
> associative array?

If arr is your associative array (i.e., an object)
In ECMAScript 5:
var keys = Object.keys(object)
In ECMAScript 3:
var keys = [];
for (var key in arr) { keys.push(key); }

/L
--
Lasse Reichstein Holst Nielsen
'Javascript frameworks is a disruptive technology'

From: Thomas 'PointedEars' Lahn on
Lasse Reichstein Nielsen wrote:

> laredotornado <laredotornado(a)zipmail.com> writes:
>> Given an associative array, how do I get an array of the keys of that
>> associative array?
>
> If arr is your associative array (i.e., an object)
> In ECMAScript 5:
> var keys = Object.keys(object)
> In ECMAScript 3:
> var keys = [];
> for (var key in arr) { keys.push(key); }

Where certain conditions must apply for the two approaches to be
equivalent, of course.

Let `o' be a reference to an object, JavaScript 1.7+ allows another
variant, Array comprehension:

var properties = [p for (p in o)];

BTW, for the property values you can use

var values = [v for each (v in o)];

there, as a combination of the ECMA-262-3 extension (v1.7+) and the
ECMA-357 implementation (v1.6+). Tested in Firefox/Iceweasel 3.5.8.


PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann