naming array values?

P

Pukeko

Hi, this is an array that is used for a dropdown menu.

var imageArray = new Array(
"ecwp://" + document.location.host + "/massey/images/massey/
massey_07_fullres.ecw",
"ecwp://" + document.location.host + "/sampleiws/images/usa/
1metercalif.ecw",
"ecwp://" + document.location.host + "/sampleiws/images/australia/
parramatta.ecw";

when the menu is displayed it shows the whole value

"ecwp://" + document.location.host + "/massey/images/massey/
massey_07_fullres.ecw""

is there a way i can just display a name for the image in the dropdown
menu, like "Massey 2007"

cheers
 
T

Tom Cole

How are you building the menu ?  If you have code to do that (and it's
always useful to include that in your post), then just adjust to suit.

Otherwise, you haven't provided enough information to answer your queston.

Tim- Hide quoted text -

- Show quoted text -

Javascript arrays are a little tricky in this regards. Yes you can
"name" and Array entry, or more accurately assign a "name" as the key.
The problem is, in my experience, that when store array elements in
this manner, the length of the array is not adjusted. For example this
is totally legit:

var images = new Array();
images['Massey 2007'] = "ecwp://" + document.location.host + "/massey/
images/massey/massey_07_fullres.ecw";
images['Massey 2006'] = "ecwp://" + document.location.host + "/massey/
images/massey/massey_06_fullres.ecw";

However if you call alert(images.length) you will get 0. Of course
even if it didn't, this wouldn't help, because you need the key (or
"name") not just the value. And there is no method that I have found
to retrieve the list of keys an array is using. So...the problem isn't
"naming" your array elements, but how do you remember that list of
names so you can later retrieve the values.

You will probably need two arrays, one to store the "names" and the
other to store the "values". How you arrange to coordinate the two are
up to you. You could simply ensure that both arrays are the same
length and that the element 0 in the names array correlates to the
element 0 in the values array, or you could keep the names in array 1
and use the names as the keys in array 2. For example:

var names = new Array();
var urls = new Array();

names.push("Massey 2007");
urls["Massey 2007"] = "ecwp://" + document.location.host + "/massey/
images/massey/massey_07_fullres.ecw";

Then you could loop through the names array and pull the url using
that value as the key from the second array. For example:

for (var i = 0; i < names.length; i++) {
var name = names;
var url = urls[name];
//build your Option here...

}

HTH...
 
U

Une Bévue

Tom Cole said:
var names = new Array();
var urls = new Array();

it is easier to use an object :

var myObject={};
myObject[name_1]=url_1;
....
myObject[name_n]=url_n;


that way, unless u've added properties to Object, using
Object.prototype, u can get all the key/value pair by

for(var key in myObject{
alert("key = "+key+", value = "+value);
}
 
T

Thomas 'PointedEars' Lahn

Pukeko said:
Hi, this is an array that is used for a dropdown menu.

From your question below I surmise you are probably talking about a `select'
element that only works with present and enabled client-side script support,
which would be a really bad idea.
var imageArray = new Array(
"ecwp://" + document.location.host + "/massey/images/massey/
massey_07_fullres.ecw",

document.location has been deprecated more than 10 years ago.
"ecwp://" + document.location.host + "/sampleiws/images/usa/
1metercalif.ecw",
"ecwp://" + document.location.host + "/sampleiws/images/australia/
parramatta.ecw";

Your source code is syntactically incorrect: the closing `)' for the
constructor call is missing. That aside, I would do at least:

var imageArray = new Array(
new Array("ecwp://", "/massey/images/massey/massey_07_fullres.ecw"),
new Array("ecwp://", "/sampleiws/images/usa/1metercalif.ecw"),
new Array("ecwp://", "/sampleiws/images/australia/parramatta.ecw")
);

for (var i = 0, len = imageArray.length; i < len; i++)
{
imageArray = imageArray.join(document.location.host);
}
when the menu is displayed it shows the whole value

"ecwp://" + document.location.host + "/massey/images/massey/
massey_07_fullres.ecw""

is there a way i can just display a name for the image in the dropdown
menu, like "Massey 2007"

Yes, there is, for example by using your own constructor:

/**
* Constructs a new <code>Item</code> object.
*
* @param uriParts: Array
* 2-element array containing the scheme and the path of
* the target URI. The parts are joined with the host name
* of the URL of the current document.
* @param text: optional string
* Text for the menu item; the default is the target URI.
* @constructor
*/
function Item(uriParts, text)
{
/**
* URI of the target resource
*/
this.uri = uriParts.join(window.location.host);

/**
* Text for the menu item
*/
this.text = text || this.uri;
}

var imageArray = new Array(
new Item(
new Array("ecwp://", "/massey/images/massey/massey_07_fullres.ecw"),
"Massey 2007"
),
...
);

You should then get informed how HTML `select' and `option' elements work,
or RTFM of your "menu" script of which you have not even posted the relevant
parts here.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Tom said:
Javascript arrays are a little tricky in this regards. Yes you can
"name" and Array entry, or more accurately assign a "name" as the key.
The problem is, in my experience, that when store array elements in
this manner, the length of the array is not adjusted.

Because you are not storing array elements then, but augment the Array object.
For example this is totally legit:

var images = new Array();
images['Massey 2007'] = "ecwp://" + document.location.host + "/massey/
images/massey/massey_07_fullres.ecw";
images['Massey 2006'] = "ecwp://" + document.location.host + "/massey/
images/massey/massey_06_fullres.ecw";

And why not? Array objects are native objects and, some properties with the
ReadOnly attribute aside, native objects can be augmented with any number of
properties.
However if you call alert(images.length) you will get 0.

It helps to use a collection implementation instead.
Of course even if it didn't, this wouldn't help, because you need the key (or
"name") not just the value. And there is no method that I have found
to retrieve the list of keys an array is using.

var a = ["x", 42];
a["foo"] = "bar";

var out = [];

for (var key in a)
{
var p = a[key];
var t = typeof p;
if (t == "string")
{
p = '"' + p.replace(/"/g, "\\$&") + '"';
}

out.push(key + ": " + t + " = " + p);
}

// displays the enumerable properties of `a'
// and the objects in its prototype chain
window.alert(out.join("\n"));

Note that this can be useful with a collection implementation; if a
collection is not required, then one should use an Object object
instead of an Array object.
So...the problem isn't "naming" your array elements, but how do you
remember that list of names so you can later retrieve the values.

Provided one does not augment Object.prototype or Array.prototype, it is not
as hard to implement as you think.
You will probably need two arrays, one to store the "names" and the
other to store the "values".

No, this is not necessary. You can create user-defined objects that are
elements of the array instead.


PointedEars
 

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
474,142
Messages
2,570,819
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top