473,753 Members | 6,232 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Array and Hash (Associative array) in JavaScript v.3.0

VK
Whatever you wanted to know about it but always were affraid to ask.

<http://www.geocities.c om/schools_ring/ArrayAndHash.ht ml>

Jul 28 '05
35 6663
I'm so confused :-)

Douglas Crockford wrote:
The simple rule is this: If all of your indexes are non-negative
integers, use an array []. Otherwise, use an object {}.


This is what I originally thought, which is why I used an array. But I'm
using for...in to iterate my array.

Having thought about this, I think it might be best to use a standard
for loop since that will be more consistent with an array, and it would
allow me to only iterate integer indexes, so that if someone messes with
Array.prototype to add Array.push or something similar, it will not get
iterated like it would with for...in.

I'll just have to figure out how to keep the indexes contiguous when I
remove an element (I'm guessing there is a way to do that using
Array.slice, instead of delete - I'll have to experiment).

Thanks for all the info, it's been fun :-)

Kevin N.
Aug 1 '05 #31
On 01/08/2005 17:31, Kevin Newman wrote:

[snip]
I'll just have to figure out how to keep the indexes contiguous when I
remove an element (I'm guessing there is a way to do that using
Array.slice, instead of delete - I'll have to experiment).


The Array.prototype .splice method[1] can be used to delete (and insert)
elements at arbitrary locations within an array. Essentially, it just
copies values about. However, this is a problem that I alluded to when I
mentioned the differences between sequential and linked data structures.
With the former, insertions and deletions at any location except the end
require all elements after the point of change to be shifted which, in a
large structure, can take a long time. With linked lists, it's simply a
matter of changing the links as the new element can be added anywhere in
memory (for insertion) or just forgotten about (for deletion).

Sequential structures offer fast, random look ups, but as yours will
always be complete iteration through the collection, this is only an
advantage when inserting or deletion at specific locations. Even then,
insertion for you will always be at the end once you've checked that the
new element is not a duplicate (more complete iteration). That just
leaves deletion, which should be rare anyway.

Despite how it may seem, I'm not necessarily pushing for you to use a
list. I do, but it's up to you to decide for yourself. Theoretical
reasons don't always in scripting, particularly as ECMAScript Arrays
don't have the same drawbacks that 'real' arrays do (including
reallocation during insertion).

Mike
[1] Not available in JScript 5 (usually IE5.0) and earlier.

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Aug 2 '05 #32
"Richard Cornford" <Ri*****@litote s.demon.co.uk> wrote in message
news:dc******** ***********@new s.demon.co.uk.. .

You are right. And Mozilla is wrong in not having the result of its
Array methods being as sparse and the original object. Though that
makes
it less reasonable of VK to assert that the array methods imply that
that arrays are continuous sequences of storage locations.

It also means that Mozilla browsers are open to 'malicious' actions
such
as:-

var a = [];
a[4294967294] = 'x';
a.reverse();

- being harmless to IE while having Mozilla grind to a halt, and
eventually cripple the entire OS.

Richard.


Not harmless in Internet Explorer 6.0.2900 here. CPU usage climbed to
90+% and stayed there for several minutes. jscript.dll version
5.6.0.8825

In Firefox 1.0.6, CPU rises to 90+% and stays there, eventually the
application generates an error and

Opera 8.02 returns almost immediately from a.reverse(), but attempting
to do -document.write( a);- results in a hung application.

--
Grant Wagner <gw*****@agrico reunited.com>
comp.lang.javas cript FAQ - http://jibbering.com/faq
Aug 2 '05 #33
"Grant Wagner" <gw*****@agrico reunited.com> writes:
"Richard Cornford" <Ri*****@litote s.demon.co.uk> wrote in message
news:dc******** ***********@new s.demon.co.uk.. .
var a = [];
a[4294967294] = 'x';
a.reverse();

Opera 8.02 returns almost immediately from a.reverse(), but attempting
to do -document.write( a);- results in a hung application.


That's because the default string representation of that array would
be an "x" followed by 4294967294 commas. You asked for it, prepare to
wait :)

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Aug 2 '05 #34


Lasse Reichstein Nielsen wrote:
Then Mozilla isn't ECMA 262 conforming, because ECMA262 v3 section
15.4.4.8 specifies the behavior of Array.prototype .reverse such that
the result should still be 2. Both Opera and IE gives 2.


The bug with reverse has been fixed recently in Mozilla so in a Mozilla
1.8 nightly or Deer Park nightly the result should be 2 too.
Here is the bug:
<https://bugzilla.mozill a.org/show_bug.cgi?id =299738>

--

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 3 '05 #35


Lasse Reichstein Nielsen wrote:

Mozilla (in my case Firefox v1.04) have other array-related bugs,
e.g.,

var a = [0,,,,,,,,,,,,,, ,,,,,19];

also gives a count of 20 elements in Firefox, but 2 in IE, Opera and
the ECMA 262 standard.


This is filed as a bug but not fixed yet:
<https://bugzilla.mozill a.org/show_bug.cgi?id =260106>

--

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 3 '05 #36

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2690
by: Robert | last post by:
I am curious why some people feel that Javascript doesn't have associative arrays. I got these definitions of associative arrays via goggle: Arrays in which the indices may be numbers or strings, not just sequential integers in a fixed range. www.sunsite.ualberta.ca/Documentation/Gnu/gawk-3.1.0/html_chapter/gawk_20.html (n.) A collection of data (an array) where individual items can be indexed (accessed) by a string, rather than by...
5
6529
by: Denis Perelyubskiy | last post by:
Hello, I need to make an array of elements accross forms. My javascript skills, as evident from this question, are rather rudimentary. I tried to make an associative array and index it with the object references. However, I just realized that indices may only be referenced by strings.
14
6703
by: Yereth Jansen | last post by:
Hi all, I encountered a problem with looping through an associative array. All worked perfectly with the following code: for (var menuItem in this.menuItems) { doSomething(); } where this.menuItems is an associative array. The problem occurred when
47
5088
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they are not at all. If you are doing *array", then you have to use only integer values for array index, as it was since ALGOL.
21
21220
by: scandal | last post by:
I am a javascript newbie working on a script that checks whether a "path" from one element in an array to another is "blocked." Currently, the script pushes an already processed cell index (hence an integer) into an array. To prevent rechecking already processed cells, the script iterates through the (sorted) array to see whether that integer is an element of the array. After reading about javascript arrays a bit more, I thought...
22
4640
by: VK | last post by:
A while ago I proposed to update info in the group FAQ section, but I dropped the discussion using the approach "No matter what color the cat is as long as it still hounts the mice". Over the last month I had enough of extra proof that the cat doesn't hount mice anymore in more and more situations. And the surrent sicretisme among array and hash is the base for it. I summarized all points in this article:...
7
39848
by: Robert Mark Bram | last post by:
Hi All! How do you get the length of an associative array? var my_cars= new Array() my_cars="Mustang"; my_cars="Station Wagon"; my_cars="SUV"; alert(my_cars.length);
104
16999
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from that array Could you show me a little example how to do this? Thanks.
30
2946
by: josh | last post by:
Hi all, what does it meaning that strange sintax (look at the object :) ? if I have i.e. array.length I can use array. and is it IE/Firefox compatible??
5
2207
by: M. Fisher | last post by:
Pardon my ignorance here... I have created arrays such as: var SDET_Lab130= new Array(); SDET_Lab130 = new Array(); SDET_Lab130 = ; SDET_Lab130 = ; SDET_Lab130 = ; SDET_Lab130 = ; SDET_Lab130 = ;
0
8896
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9653
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9451
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9421
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8328
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4771
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4942
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3395
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2872
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.