473,320 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Hash array without using objects

I could see that it is possible to have hash array using objects like
var hash = {"a" : "1", "b" : "2"};

Couldn't still findout how to declare hash array in Array.
var arr = new Array("a" : "1", "b" : "2"); doesn't work. Any hints? TIA

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Jul 23 '05 #1
5 1976
On 9 Dec 2004 11:20:33 -0800, R. Rajesh Jeba Anbiah
<ng**********@rediffmail.com> wrote:
I could see that it is possible to have hash array using objects like
var hash = {"a" : "1", "b" : "2"};
It's not strictly a hash table. What you have above is an object literal
that contains a property/value list. The following snippets are equivalent:

var hash = {a : '1', b : '2'};

var hash = {}; // or new Object();
hash.a = '1';
hash.b = '2';
Couldn't still findout how to declare hash array in Array.
Why would you want to? There's no benefit. But to answer your question,
you'll have to add the properties one by one:

var arr = []; // or new Array();

arr.a = '1';

or

arr['b'] = '2';

[snip]
--


Signature separators are two dashes followed by a space. You seem to have
omitted the space.

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #2
Michael Winter wrote:
On 9 Dec 2004 11:20:33 -0800, R. Rajesh Jeba Anbiah
<ng**********@rediffmail.com> wrote:
I could see that it is possible to have hash array using objects like var hash = {"a" : "1", "b" : "2"};
It's not strictly a hash table. What you have above is an object

literal that contains a property/value list. The following snippets are equivalent:

Thanks for pointing out that. When I quickly searched for hash array
implementation in JS, I came across that. Didn't know that it's object
and properties.
var hash = {a : '1', b : '2'};

var hash = {}; // or new Object();
hash.a = '1';
hash.b = '2';
Couldn't still findout how to declare hash array in Array.
Why would you want to? There's no benefit. But to answer your

question, you'll have to add the properties one by one:
Yes, it doesn't work--both Array and ordinary objects work same in
hash thing. I'd thought in Array, we can use reverse() method and
length property as I need to reverse the hash array--but to my
surprise, it doesn't work--even though Array has reverse() method.

Basically, I want to translate certain characters and implementing
something like PHP's strtr <http://in.php.net/strtr>. In one occasion,
I need to translate characters using map table in forward order, but in
other case need to do in reverse order. I'd thought that hash
implementation would be better in this case as it provides better
readability, etc. In JS, forward thing works fine, but couldn't reverse
such hash array. Any idea? [snip]
--
Signature separators are two dashes followed by a space. You seem to

have omitted the space.


It's not me, but stupid GG Beta; I'm tired of reporting bugs as they
don't care about anything. Sounds like they're saving bits and bytes to
offer GB for their Gmail;)

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Jul 23 '05 #3
"R. Rajesh Jeba Anbiah" <ng**********@rediffmail.com> wrote:
I'd thought in Array, we can use reverse() method and length
property as I need to reverse the hash array--but to my surprise, it
doesn't work--even though Array has reverse() method.


Sure reverse() works. It just does not work the way you thing it would.

a = new Array(1,2,3); // a[0] = 1, a[1] = 2, a[2] = 3
a.reverse( ); // now a[0] = 3, a[1] = 2, a[2] = 1

You can't swap indices and values (if this is what you want to do) in
an Array(), because indices can only be interger and values can be of
any type.

Jul 23 '05 #4
Martin Bialasinski wrote:
"R. Rajesh Jeba Anbiah" <ng**********@rediffmail.com> wrote: <snip> Just as 111111=>aaaaaa. Or 1113=>aaaaaa.

But, if I use the *same* translation map to "untranslate" "aaaaaa",
it becomes:
aaaaaa=>111111

_unless_ I change the order of map and reverse the string.
But you can't "untranslate" it, because your translation function

does not do a unambiguous transformation.

You can't know what the value was that became "aaaaaa". If it was 123, 111111 or 1113. Each of then give "aaaaaa" after translation.


Yes, you're right. Reversing the map table wouldn't help for
ambiguous transformation. Thanks for pointing out.

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Jul 23 '05 #5
Hi,

Michael Winter wrote:
Why would you want to? There's no benefit. But to answer your question,
you'll have to add the properties one by one:

var arr = []; // or new Array();

arr.a = '1';

or

arr['b'] = '2';

[snip]


Note however that creating an array is not useful in this case, since
you don't use any of the Array features, but only (mis)use the Array as
an Object to store properties in it. There is no added value in using an
Array for this.

HTH,

Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch

Jul 23 '05 #6

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

Similar topics

31
by: Alexis Nikichine | last post by:
Hello, It is common knowledge that arrays can be used as hashtables: var color = ; color = 0xFF0000; color = 0x0000FF;
47
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...
22
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...
35
by: VK | last post by:
Whatever you wanted to know about it but always were affraid to ask. <http://www.geocities.com/schools_ring/ArrayAndHash.html>
38
by: VK | last post by:
Hello, In my object I have getDirectory() method which returns 2-dimentional array (or an imitation of 2-dimentional array using two JavaScript objects with auto-handled length property - please...
12
by: wxs | last post by:
Many times we have a bunch of enums we have from either different enums or the same enum that will have various numeric values assigned. Rarely will there be collisions in numbering between the...
13
by: Sam Kong | last post by:
Hi, I am familiar with the ruby language. To use my ruby knowledge when writing in JavaScript, I use the following library. http://www.advogato.org/proj/Ruby.js/ If I include the file in...
9
by: IamIan | last post by:
I'm using an array to store map features (name, lat, lon, caption, etc), from which the user can then select an individual feature. The problem is that when thousands of features are stored in the...
23
by: raylopez99 | last post by:
A quick sanity check, and I think I am correct, but just to make sure: if you have a bunch of objects that are very much like one another you can uniquely track them simply by using an ArrayList...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.