...

View Full Version : how to avoid array pop effect



aku
02-08-2007, 08:11 PM
How can i avoid the array's pop effect that executes pop on array1 too ?


<script type="text/javascript">
var array1 = new Array() ;
var array2 = new Array() ;
array1[0] = 'foo' ;
array1[1] = 'miao' ;
array1[2] = 'grrr' ;
array2 = array1 ;
array2.pop() ;
</script>

FJbrian
02-08-2007, 08:38 PM
I'm thinking delete that line but that's obvious so....please explain further

aku
02-08-2007, 08:45 PM
<script type="text/javascript">
var array1 = new Array() ;
var array2 = new Array() ;
array1[0] = 'foo' ;
array1[1] = 'miao' ;
array1[2] = 'grrr' ;
array2 = array1 ;
array2.pop() ;
</script>

array2 = array1 ;
array2.pop() ;

I make pop() on array2, but the effect of pop is propagated on array 1 too.
instead i want that array1 rest unchanged!
Isn't clear ?

FJbrian
02-08-2007, 08:46 PM
you made em' equal on the previous line though

aku
02-08-2007, 08:55 PM
ok! maybe you don't read what i'm writing...

array2.pop() ; -> last element of array been discarded

when i made this on ARRAY2 the same thing happen in ARRAY1!

when i assing array1 TO array2 the array have all elements, ok?

after the script ALL arrays have an element less, but i made the function to discard an element ONLY in array2.

how can i have array1 with all data AND an array2 with the result pop ?

FJbrian
02-08-2007, 09:00 PM
lemme see the pop function referenced please

FJbrian
02-08-2007, 09:02 PM
scratch that, I know what ya mean
http://www.w3schools.com/jsref/jsref_pop.asp

you want to use shift not pop

aku
02-08-2007, 09:10 PM
no thanks, i dont want use shift ( shift discard first element ).

FJbrian
02-08-2007, 09:35 PM
no thanks, i dont want use shift ( shift discard first element ).


please see link
pasted:
Definition and Usage
The pop() method is used to remove and return the last element of an array.

Syntax
arrayObject.pop()


--------------------------------------------------------------------------------

Tips and Notes
Note: This method changes the length of the array.

Tip: To remove and return the first element of an array, use the shift() method.

aku
02-08-2007, 09:39 PM
ok
read here (http://www.webdeveloper.com/forum/showthread.php?p=710913#post710913) and MAYBE you'll understand what i mean...

david_kw
02-08-2007, 09:49 PM
The problem is that array1 and array2 are the same the way you have defined them. The variable "array1" and "array2" are just references for the array object with the data in it. What you want to do is copy the array instead of just making a new reference to it.

I've never done it but I believe the concat() function is the recommended way of copying an array. So you'd say

var array2 = array1.concat([]); // make a new copy of array1 then add nothing to the end

Try it out. Hopefully that won't tack a null on the end of your array. I don't think it will.

david_kw


I also see that slice() can be recommended. I'd probably use slice() on second thought.

var array2 = array1.slice(0);

Now you have two options to try.

aku
02-08-2007, 09:58 PM
yeah this is a nice answer!! thanks david :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum