Hash array without using objects

  • Thread starter R. Rajesh Jeba Anbiah
  • Start date
R

R. Rajesh Jeba Anbiah

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
 
M

Michael Winter

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
 
R

R. Rajesh Jeba Anbiah

Michael said:
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';


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;)
 
M

Martin Bialasinski

R. Rajesh Jeba Anbiah said:
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.
 
M

Michael Winter

Michael said:
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. [...]

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.

This appears to be a hot topic at the moment.

[snip]
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.

To get an accurate length value, you'd have to write your own object
implementation that kept track of that information for you. As for
the reverse method, it's not really applicable. Hash tables don't
return values in any particular order, so reversing that order
doesn't make much sense.

[snip]
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'm not quite sure I follow. Do you mean that with

var hash = {a : 'b'};

you'd want

hash['a']

to yield 'b', and

hash['b']

to yield 'a'? That simply isn't possible, and the reverse method
doesn't do that anyway; it swaps the elements in an n-length array
so that element 0 becomes n-1, 1 -> n-2, 2 -> n-3, ..., n-2 -> 1,
n-1 -> 0. Assuming that I am correct, you would have to repeat the
entries:

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

[snip]

Mike
 
R

R. Rajesh Jeba Anbiah

Michael Winter wrote:
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'm not quite sure I follow. Do you mean that with

var hash = {a : 'b'};

you'd want

hash['a']

to yield 'b', and

hash['b']

to yield 'a'?
<snip>

Sorry, I didn't mean about flipping array or swapping keys with
values. What I meant was totally reversing the order of array.

For example, if I have the translation map as:
1=>a
2=>aa
3=>aaa

If the string to tranlate is "123", it becomes:
123=>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.

However, I findout that the best way (IMHO) is to go for two arrays
(numerical indices arrays) for such translation map.

Many thanks to all the people who shared your ideas and time.
 
M

Martin Bialasinski

R. Rajesh Jeba Anbiah said:
For example, if I have the translation map as:
1=>a
2=>aa
3=>aaa

If the string to tranlate is "123", it becomes:
123=>aaaaaa

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.
 
R

R. Rajesh Jeba Anbiah

Martin said:
"R. Rajesh Jeba Anbiah" <[email protected]> wrote:
Just as 111111=>aaaaaa. Or 1113=>aaaaaa.

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.
 
L

Laurent Bugnion

Hi,

Michael said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top