Discuss Arrays in the JavaScript Development forum on Dev Shed. Arrays JavaScript Development forum discussing JavaScript and DHTML, AJAX, and issues such as coding cross-browser JavaScript.
The ASP Free website provides in-depth information on the latest developer tools available from Microsoft. Our cadre of writers, highly experienced industry experts, reveals the best ways to use established technologies as well as new and emerging technologies. Our coverage of Microsoft's development and administration technologies is among the most respected in the IT industry today.
ASP Free and Iron Speed Designer are giving away $5,500+ in FREE licenses. Iron Speed's RAD CASE toolset can save up to 80% of your coding time. One free license per week, one perpetual license per month! Download and Activate to enter!
Intel® Graphics Performance Analyzers is a powerful tool suite for analyzing and optimizing your games, media, and graphics-intensive applications. Used by some of the best developers on the planet, Intel GPA lets you maximize your app’s performance.
Posts: 84
Time spent in forums: 1 Day 1 h 24 m 13 sec
Reputation Power: 7
Arrays
I can't seem to get the hang of arrays in javascript, but I feel like I need 'em, 'cause daaaang this is ugly and impractical(and just a portion of the entire code, something like 200 elements) can anyone tell me how to do? (Yes this is one of the few times that I actually do want some code examples)
this is a multidimensional array (aka arrays within arrays)
the main array with every thing in it is mover and it is divided by [letter] then [letter] is divided by [number] and number has two values [height] and [width]
so...
mover[a][1][25px] = (the mover a1's height is 25px)
mover[a][1][50px] = (the mover a1's width is 50px)
mover[a][2][25px] = (the mover a2's height is 25px)
mover[a][2][50px] = (the mover a2's width is 50px)
mover[b][1][25px] = (the mover b1's height is 25px)
mover[b][1][50px] = (the mover b1's width is 50px)
i'm a visual learner so I think of this as...
Code:
Array mover (
-> Array a (
-> Array 1 (
-> 25px
-> 50px
)
-> Array 2 (
-> 25px
-> 50ps
)
)
-> Array b (
-> Array 1 (
-> 25px
-> 50px
)
)
)
Last edited by aconway : November 19th, 2005 at 10:57 AM.
this is a multidimensional array (aka arrays within arrays)
the main array with every thing in it is mover and it is divided by [letter] then [letter] is divided by [number] and number has two values [height] and [width]
so...
mover[a][1][25px] = (the mover a1's height is 25px)
mover[a][1][50px] = (the mover a1's width is 50px)
mover[a][2][25px] = (the mover a2's height is 25px)
mover[a][2][50px] = (the mover a2's width is 50px)
mover[b][1][25px] = (the mover b1's height is 25px)
mover[b][1][50px] = (the mover b1's width is 50px)
i'm a visual learner so I think of this as...
Code:
Array mover (
-> Array a (
-> Array 1 (
-> 25px
-> 50px
)
-> Array 2 (
-> 25px
-> 50ps
)
)
-> Array b (
-> Array 1 (
-> 25px
-> 50px
)
)
)
sorry it took me so long to answer to the post, been away for a few days...just wanted to say thanx...big time! You have no idea how greatful I am, I really apreciate your help!
Man this made me realise that I have such a long way to go...can some kind soul please load this code with remarks? The parts that I don't get are particularly the
Code:
var W3= document.getElementById;
var IE= document.all; // Opera7 as well
var NN= document.layers;
Posts: 2,179
Time spent in forums: 2 Weeks 2 Days 15 h 42 m 16 sec
Reputation Power: 56
Seeing that I am the one who provided the code, maybe I should
also be the one to describe it's usess.. though I'm sure others
are capable as well.
Code:
//------------------------------------------------------------------------
// Object Detection:
//------------------------------------------------------------------------
// Object detection is the testing of an object, property, or method to
// detect that a certain object, property, or method exists before using
// to releave a particular browser's user from countless errors.
var W3= document.getElementById;
var IE= document.all; // Opera7 as well
var NN= document.layers;
//------------------------------------------------------------------------
// function getObj()...
//------------------------------------------------------------------------
// Here is a function that returns a reference to an object depending on
// which browser they are using:
// For Example:
// W3 -- IE6, NN6, MozFF, Konqueror, Safari, ...
// IE -- IE, Opera7, ...
// NN -- NN, ...
function getObj(ref){
return((W3) ? document.getElementById(ref) :
(IE) ? document.all[ref] :
(NN) ? document.layers[ref] : null);
}
//------------------------------------------------------------------------
// function assign_Val()...
//------------------------------------------------------------------------
// This function takes the values assigned to each parameter and assigns
// them to each object specified in the array (assigned to the arr parameter).
//
function assign_Val(n,arr,attr){
// kcuf -- assumedly the width
// ykcuf -- assumedly the height
var kcuf='100px', ykcuf='100px';
// loops through the paramter arr (an array) and counts down backwards from
// the arrays length to zero (as KorRed Devil suggested is faster)
for (var i= arr.length; i >= 0; i--){
// loops through the parameter attr backwards, prepping the assignment
// of the 'width' or 'height'
for (var j= attr.length; j >= 0; j--){
// the paramter n is the number of objects that are in each group of
// the arr paramter.
// For Example:
// a1mover, a2mover, a3mover, a4mover, b1mover, b2mover, ...
for (var zero=0; n > zero; n--){
// Refer to the getObj function description above
//----------------------------------------------------------------
// .style[]
//----------------------------------------------------------------
// All objects have multiple methods to access them, or notations
// to visually format the access.
// - attr[j] could be either 'width' or 'height'
// the equivalent to the below notation is: ...style.width= ... but
// because we are not directly accessing the width or height, we must
// use this particular notation.
getObj(arr[i]+n+'mover').style[ attr[j] ]= ((attr[j] == 'width') ? kcuf : ykcuf);
}
}
}
}
This part, I felt, deserved another section to itself so..
Basically this code is an if/else statement, though formatted
in a different notation.
Code:
((attr[j] == 'width') ? kcuf : ykcuf);
If you wanted it in if/else format it would be more redundant.
Posts: 2,179
Time spent in forums: 2 Weeks 2 Days 15 h 42 m 16 sec
Reputation Power: 56
Most objects, properties, and methods in javascript especially have
multiple ways to be accessed due to browser incompatibilities over
the years... so many browsers have gone and taken the extra steps
to include backwards compatibility and cross browser solutions...
though in this case, it is merely for ease of using arrays.
These are exactly the same thing... though the second can be
used with more ease, as far as I am concerned...
My 2 cents on browser detection. I think setting up vars that indicate what sort of browser is running is bad. In this particular instance you won't run into any problems but if you rely on that detection to branch other scripts you could hit issues.
The danger is that you might hit a browser that supports document.all but isn't IE. Having decided that the browser is IE your script then tries to use IE only methods which could then fail.
If I was going to write a getObj script it I'd skip the browser detection and just use object detection within the function.
Posts: 2,179
Time spent in forums: 2 Weeks 2 Days 15 h 42 m 16 sec
Reputation Power: 56
Well--you're right. It could be misleading and lead me/others into
unexpected erros with the way that I was using my getObj function
except that is where experience comes into play.
I only use that setup cause I know what I am using works with
certain browsers for sure... every now and again I have to ask,
but even so... you're "ways" in general seem to be better.