Forums > Javascript

pass an array

October 18, 2006 | 2:29 pm

hi all, i am trying to pass an array without changing the original, how?
i came across some knowledge that suggested doing it inside a function but that doesn’t help.
i workaround is to assign the same information to the two arrays (at creation) but that is no good.

var Sort=["aoo","coo","boo"];
var outOrder=new Array();

function bang()
{
post("Sort: "+Sort+"n");
sorty(Sort);
post("Sort: "+Sort+"n");
}
function sorty(vvv){
outOrder=vvv
outOrder.sort()
post("outOrder: "+outOrder+"n");
}


October 18, 2006 | 2:52 pm

converting to string and back works, but mybe there is other way?

cvSort=["aoo","coo","boo"];
function bang()
{

post("cvSort: "+cvSort+"n");
outOrder=cvSort.join();
outOrder=outOrder.split(",");
outOrder.sort()
post("outOrder: "+outOrder+"n");
post("cvSort: "+cvSort+"n");
//outlet(0,"myvalue","is",myval);
}

2006/10/18, yair reshef :
>
>
> hi all, i am trying to pass an array without changing the original, how?
> i came across some knowledge that suggested doing it inside a function but
> that doesn’t help.
> i workaround is to assign the same information to the two arrays (at
> creation) but that is no good.
>
>
> var Sort=["aoo","coo","boo"];
> var outOrder=new Array();
>
> function bang()
> {
> post("Sort: "+Sort+"
> ");
> sorty(Sort);
> post("Sort: "+Sort+"
> ");
> }
> function sorty(vvv){
> outOrder=vvv
> outOrder.sort()
> post("outOrder: "+outOrder+"
> ");
> }
>


October 18, 2006 | 3:06 pm

On 18 oct. 06, at 16:29, yair reshef wrote:

> hi all, i am trying to pass an array without changing the original,
> how?
> i came across some knowledge that suggested doing it inside a
> function but that doesn’t help.

You need to copy your array. As far as I know, there’s no JavaScript
built-in method to do that, so it have to be manual :-(

ej


October 18, 2006 | 5:24 pm

Well, the first hit in Google for "javascript array copy" is:

http://www.sematopia.com/?p=12

So calling:

newArray = oldArray.slice(0);

should do the trick.
(It takes a "slice" of the array, beginning at the first element all
the way to the end.)

Steve

On 10/18/06, Emmanuel Jourdan wrote:
> On 18 oct. 06, at 16:29, yair reshef wrote:
>
> > hi all, i am trying to pass an array without changing the original,
> > how?
> > i came across some knowledge that suggested doing it inside a
> > function but that doesn’t help.
>
> You need to copy your array. As far as I know, there’s no JavaScript
> built-in method to do that, so it have to be manual :-(
>
> ej
>
>
>


October 18, 2006 | 5:33 pm

thank you, ive red the slice ref 10 times and didnt see this.
zen time:
"not the question but the search string that counts"

2006/10/18, Stephen Sinclair :
>
> Well, the first hit in Google for "javascript array copy" is:
> http://www.sematopia.com/?p=12
>
> So calling:
>
> newArray = oldArray.slice(0);
>
> should do the trick.
> (It takes a "slice" of the array, beginning at the first element all
> the way to the end.)
>
> Steve
>
>
> On 10/18/06, Emmanuel Jourdan wrote:
> > On 18 oct. 06, at 16:29, yair reshef wrote:
> >
> > > hi all, i am trying to pass an array without changing the original,
> > > how?
> > > i came across some knowledge that suggested doing it inside a
> > > function but that doesn’t help.
> >
> > You need to copy your array. As far as I know, there’s no JavaScript
> > built-in method to do that, so it have to be manual :-(
> >
> > ej
> >
> >
> >
>


October 18, 2006 | 5:43 pm

Ha, no worries. If JS simply had an array.copy() function it would
have been obvious. I find it has many oddities, I’m always having to
google for things when programming in javascript. By now, I’m
convinced that it’s actually quite a powerful language, but certainly
not an obvious one.

Steve

On 10/18/06, yair reshef wrote:
>
> thank you, ive red the slice ref 10 times and didnt see this.
> zen time:
> "not the question but the search string that counts"
>
> 2006/10/18, Stephen Sinclair :
> > Well, the first hit in Google for "javascript array copy" is:
> > http://www.sematopia.com/?p=12
> >
> > So calling:
> >
> > newArray = oldArray.slice(0);
> >
> > should do the trick.
> > (It takes a "slice" of the array, beginning at the first element all
> > the way to the end.)
> >
> > Steve
> >
> >
> > On 10/18/06, Emmanuel Jourdan wrote:
> > > On 18 oct. 06, at 16:29, yair reshef wrote:
> > >
> > > > hi all, i am trying to pass an array without changing the original,
> > > > how?
> > > > i came across some knowledge that suggested doing it inside a
> > > > function but that doesn’t help.
> > >
> > > You need to copy your array. As far as I know, there’s no JavaScript
> > > built-in method to do that, so it have to be manual :-(
> > >
> > > ej
> > >
> > >
> > >
> > >
> >
> >
>
>
>
>
>


Viewing 6 posts - 1 through 6 (of 6 total)