Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > How do I get keys from an associative array?

Reply
Thread Tools

How do I get keys from an associative array?

 
 
laredotornado
Guest
Posts: n/a
 
      02-21-2010
Hi,

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

Thanks, - Dave
 
Reply With Quote
 
 
 
 
David Mark
Guest
Posts: n/a
 
      02-21-2010
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").
 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      02-21-2010
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$(E-Mail Removed)>
 
Reply With Quote
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      02-21-2010
laredotornado <(E-Mail Removed)> 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'

 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      02-21-2010
Lasse Reichstein Nielsen wrote:

> laredotornado <(E-Mail Removed)> 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
 
Reply With Quote
 
Dr J R Stockton
Guest
Posts: n/a
 
      02-25-2010
In comp.lang.javascript message <(E-Mail Removed)>, Sun, 21 Feb
2010 04:27:12, Lasse Reichstein Nielsen <(E-Mail Removed)> posted:
>laredotornado <(E-Mail Removed)> 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); }


The second entry should I suppose be headed "In ECMAScript 3 & 5"; and
will then be the way to code it (for the open Web) until all browsers in
use are sufficiently ES 5 compliant.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (RFCs 5536/7)
Do not Mail News to me. Before a reply, quote with ">" or "> " (RFCs 5536/7)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Why "associative" in associative container? desktop C++ 5 06-26-2007 07:49 AM
Generics and associative array of keys and diverse objects Frank Fredstone Java 1 09-19-2006 11:42 AM
sorting an associative array keys based on values soup_or_power@yahoo.com Javascript 5 06-21-2005 01:05 AM
keyboard keys replacing mouse keys? larry Computer Support 8 09-14-2003 07:32 PM



Advertisments