Using an array to reference a different array

A

Andrew Poulos

Say I create an array like this:

var p = [];
p[0] = [];
p[0][0] = ["0","1","2","3"];
p[0][1] = ["4","5","6","7"];
p[0][2] = ...
p[1] = [];
p[1][1] = ...

and I've a method which will return the position of the first found
element specified, as an array

Array.prototype.findValue = function(v) {
// blah
};

so

p.findValue("4")

would return

[0,1,1]

how can I then refer to the (sub) array that the return value refers to?

I can pop the return to get [0,1] but I don't know how to translate that
to p[0][1] without building it up manually at the time I need it.

I wishfully thought this might work p[0,1].

Andrew Poulos
 
J

Jorge

Say I create an array like this:

var p = [];
p[0] = [];
p[0][0] = ["0","1","2","3"];
p[0][1] = ["4","5","6","7"];
p[0][2] = ...
p[1] = [];
p[1][1] = ...

and I've a method which will return the position of the first found
element specified, as an array

Array.prototype.findValue = function(v) {
   // blah

};

so

p.findValue("4")

would return

[0,1,1]

how can I then refer to the (sub) array that the return value refers to?

I can pop the return to get [0,1] but I don't know how to translate that
to p[0][1] without building it up manually at the time I need it.

I wishfully thought this might work p[0,1].

Andrew Poulos

var p = [];
p[0] = [];
p[0][0] = ["0","1","2","3"];
p[0][1] = ["4","5","6","7"];
var found= [0,1,1];

var i, e;
while (found.length) {
i= found.shift();
e= (e && e) || p;
}
alert(e);
 
S

SAM

Le 12/22/08 11:35 PM, Andrew Poulos a écrit :
Say I create an array like this:

var p = [];
p[0] = [];
p[0][0] = ["0","1","2","3"];
p[0][1] = ["4","5","6","7"];
p[0][2] = ...
p[1] = [];
p[1][1] = ...

and I've a method which will return the position of the first found
element specified, as an array

Array.prototype.findValue = function(v) {
// blah
};

so

p.findValue("4")

would return

[0,1,1]

[0,1,0] // No ?
how can I then refer to the (sub) array that the return value refers to?

var a = p[0][1][0];
alert(a); // --> 4


var o = p.findValue('4');

var a = p[o[0]][o[1]][o[2]];
alert(a); // --> 4

function getValue(arr) {
var a = ''
for(var i in arr) a += '['+arr+']';
return eval('p'+a);
}
alert(getValue(o)); // --> 4

function getValue(arr) { return p[arr[0]][arr[1]][arr[2]]; }
alert(getValue(o)); // --> 4
 
T

Thomas 'PointedEars' Lahn

Andrew said:
Say I create an array like this:

var p = [];
p[0] = [];
p[0][0] = ["0","1","2","3"];
p[0][1] = ["4","5","6","7"];
[...]

and I've a method which will return the position of the first found
element specified, as an array

Array.prototype.findValue = function(v) {
// blah
};

Note that if you do this you would also need a method that handles for..in
iteration on Array objects.
so

p.findValue("4")

would return

[0,1,1]

Should it not be [0, 1, 0] since "4" is at the first position (index 0)?
how can I then refer to the (sub) array that the return value refers to?

Why not simply return a reference to the "sub array" and the index of the
element in it? Since ECMAScript functions unfortunately cannot return
several values, return a reference to an Array object storing them.
For example:

return [aNeedle, indexInAneedle];
I can pop the return to get [0,1] but I don't know how to translate that
to p[0][1] without building it up manually at the time I need it.

If you are referring to a loop "building it up manually",
Array.prototype.join() and eval() could serve instead:

var a = [0, 1, 1];
a.pop();
var a2 = eval("p['" + a.join("']['") + "']");

But I strongly suggest you return a proper value instead.
I wishfully thought this might work p[0,1].

If wishes were horses ...

p["0,1"] could be implemented, though.


PointedEars
 
J

Jorge

Say I create an array like this:
var p = [];
p[0] = [];
p[0][0] = ["0","1","2","3"];
p[0][1] = ["4","5","6","7"];
p[0][2] = ...
p[1] = [];
p[1][1] = ...
and I've a method which will return the position of the first found
element specified, as an array
Array.prototype.findValue = function(v) {
   // blah
p.findValue("4")

would return

how can I then refer to the (sub) array that the return value refers to?
I can pop the return to get [0,1] but I don't know how to translate that
to p[0][1] without building it up manually at the time I need it.
I wishfully thought this might work p[0,1].
Andrew Poulos

var p = [];
p[0] = [];
p[0][0] = ["0","1","2","3"];
p[0][1] = ["4","5","6","7"];
var found= [0,1,1];

var i, e;
while (found.length) {
  i= found.shift();
  e= (e && e) || p;}

alert(e);


Or, better yet:

var p= [[["0","1","2","3"],["4","5","6","7"]]];

p.extract= function (p) {
var e= this[p.shift()];
while (p.length) { e= e[p.shift()]; }
return e;
};

alert(p.extract([0,1,1])); //--> "5"
 
D

Dr J R Stockton

In comp.lang.javascript message <49501651$0$15741$5a62ac22@per-
qv1-newsreader-01.iinet.net.au>, Tue, 23 Dec 2008 09:35:25, Andrew
Poulos said:
p.findValue("4")

would return

[0,1,1]

how can I then refer to the (sub) array that the return value refers
to?

It might be better if findValue were to return [p[0], p[0][1], 4]
or [0, p[0], 1, p[0][1], 4].

It might also be better if findValue were to return an Object, so that
result parts could be referred to by name rather than by number.
 
J

Jorge

Say I create an array like this:
var p = [];
p[0] = [];
p[0][0] = ["0","1","2","3"];
p[0][1] = ["4","5","6","7"];
p[0][2] = ...
p[1] = [];
p[1][1] = ...
and I've a method which will return the position of the first found
element specified, as an array
Array.prototype.findValue = function(v) {
   // blah
p.findValue("4")

would return

how can I then refer to the (sub) array that the return value refers to?
I can pop the return to get [0,1] but I don't know how to translate that
to p[0][1] without building it up manually at the time I need it.
I wishfully thought this might work p[0,1].
Andrew Poulos

var p = [];
p[0] = [];
p[0][0] = ["0","1","2","3"];
p[0][1] = ["4","5","6","7"];
var found= [0,1,1];

var i, e;
while (found.length) {
  i= found.shift();
  e= (e && e) || p;}

alert(e);


Or, better yet:

var p= [[["0","1","2","3"],["4","5","6","7"]]];

p.fetch= function (p) {
var e= this[p.shift()];
while (p.length) { e= e[p.shift()]; }
return e;
};

alert(p.fetch([0])); //--> [["0","1","2","3"],["4","5","6","7"]]
alert(p.fetch([0,1])); //--> ["4","5","6","7"]
alert(p.fetch([0,1,1])); //--> "5"
 

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

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top