/    Sign up×
Community /Pin to ProfileBookmark

Accessing unknown arrays

Hi,

Is there any way in javascript to access a number of arrays if

1) you don’t know how many arrays there are and
2) you don’t know their names

This might sound strange, but I’m sourcing a .js file which contains a number of arrays, but I don’t know beforehand how many and what their names are (this will change from site to site) and in the .js file which sources this file, I want to build a function which can operate on these arrays, and I don’t want to change this function for every site. I want to build a general function which can loop through the arrays from the .js file that has been sourced. I’m thinking maybe I can access these arrays through the DOM, but I can’t figure out how.

TIA
Peter

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@BigMoosieAug 27.2005 — Strange issue...

First off you cannot access them through the DOM.

The .js file will contain functions that may create arrays, but they do not exist as Arrays until they are parsed, perhaps you should show us an example of one of these .js files you wish to source out so I can see what they're like.
Copy linkTweet thisAlerts:
@ptrlsnauthorAug 27.2005 — Hi,

Here's an example .js file which contains some arrays (actually arrays of arrays)

var Array1 = [ ["Item1","URI1",""],

["Item2","URI2",""],

["Item3","URI3",""]

];

var Array2 [ ["Item1","URI1","x"],

["Item2","URI2","x"],

["Item3","URI3","x"]

];

Now, in the file that sources this file, I want a function and inside that I want a looping construct, which grabs any of the two arrays and builds a menu system using div-tags, then grabs the next one and does the same thing to that. And the same again for any number of arrays that might be in the .js file containing the arrays. However I want to generalize this, so I can use it on any page without having to write a new menu-building-function every time, but to do that, I need to be able to grab the arrays and process them without knowing beforehand how many arrays there are and what they are called. Maybe an array isn't the best choise? Please feel free to give suggestions

TIA

Peter
Copy linkTweet thisAlerts:
@BigMoosieAug 27.2005 — Well, once you manage to get your arrays into your container array then this function will make a visual representation of your arrays:

Array.prototype.toString=function(){
var str='<OL start="0">', i=0;
while (i<this.length) str+="<LI>"+this[i++]+"</LI>";
return str+"</OL>";
}
// example:
var a=[0,1,[0,1,[0,1,2],2],2];
document.write(a);
Copy linkTweet thisAlerts:
@vwphillipsAug 28.2005 — was hoping there would be a better answer to this

but think you will have to add an additional array containing the arrays to the js with the arrays.

[CODE]

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>
<title></title>
<script language="JavaScript" type="text/javascript">
<!--
var tom=new Array('a','b');
var dick=new Array('c','d');

var fred=new Array(tom,dick);



//-->
</script>
<script language="JavaScript" type="text/javascript">
<!--
function ff(){
if (window['fred']){
alert(fred.length+' arrays exist in array fred')
}
}

//-->
</script>

</head>

<body onload="ff();" >

</body>

</html>

[/CODE]
×

Success!

Help @ptrlsn spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.8,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...