Closed Thread Icon

Topic awaiting preservation: Array related question Pages that link to <a href="https://ozoneasylum.com/backlink?for=8507" title="Pages that link to Topic awaiting preservation: Array related question" rel="nofollow" >Topic awaiting preservation: Array related question\

 
Author Thread
Archonian
Nervous Wreck (II) Inmate

From: Sweden, Malmoe
Insane since: Jan 2003

posted posted 02-02-2003 16:12

Hello,

I was wondering, is there any built in function that easily switches two array elements, so that if:
something = new Array(2,1,3,5,1);
and I wanted to switch places with element 0 and 1.
So the array becomes 1 2 3 5 1;

I know I can do:
temp = something[0];
something[0] = something[1];
something[1] = temp;

but is there any faster way?

---

Also if I had an array with say 50 elements, they haven't been defined and it was created like:
something = new Array(50);
The array element doesn't take up any memory until they have been defined, right?
So, if instead of having them undefined I decided to give the elements the value 0 then that would be a waste of space?

Thanks in advance.

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 02-02-2003 17:43

"I was wondering, is there any built in function that easily switches two array elements"

I don't think there's a built in function for that. You could always write your own, of course.

"Also if I had an array with say 50 elements, they haven't been defined and it was created like:
something = new Array(50);
The array element doesn't take up any memory until they have been defined, right?"

I think that's right. Then again, it could depend on the browser running the script. However, I think it's a safe assumption.

Archonian
Nervous Wreck (II) Inmate

From: Sweden, Malmoe
Insane since: Jan 2003

posted posted 02-02-2003 20:57

I tried searching the net on how to empty an array but all I came up with was to loop it through and set an empty value for each element.

If there's no such function, could I use:
for (var i=0; i<35; i++) {
for (var j=0; j<50; j++) {
delete MAP[i][j];
}
}
or I just need to delete the first array? i.e. delete MAP[i];
and what would that do exactly?

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 02-02-2003 21:20

To empty an array:

myarray.length = 0;

Bmud
Bipolar (III) Inmate

From: Raleigh, NC
Insane since: Mar 2001

posted posted 02-02-2003 22:07

ooh. that's cool slime. =D My mind is boggling

Shine and shine.
[Cell=992]

Clay
Nervous Wreck (II) Inmate

From: Utreg, NL
Insane since: Nov 2002

posted posted 02-03-2003 09:14

If you'd need a swap function for arrays at various places in your code, you could add a "swap" (or something else) to the array prototype, for example;

code:
Array.prototype.swap = function(a, b) {
var temp = this[a];
this[a] = this[b];
this[b] = temp;
}

var blah = [1,2,3,4];
blah.swap(1,2)

alert(blah);



This way any array will have a swap method, which you can feed two indices of the elements you want to be swapped (starting at 0 for 1st element).

peterned

Archonian
Nervous Wreck (II) Inmate

From: Sweden, Malmoe
Insane since: Jan 2003

posted posted 02-04-2003 21:57

Thanks Slime and Clay. I did not know of prototype...

« BackwardsOnwards »

Show Forum Drop Down Menu