Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    aku
    aku is offline
    New Coder
    Join Date
    Jan 2007
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    how to avoid array pop effect

    How can i avoid the array's pop effect that executes pop on array1 too ?

    Code:
    <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>

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    Adirondacks
    Posts
    516
    Thanks
    4
    Thanked 4 Times in 4 Posts
    I'm thinking delete that line but that's obvious so....please explain further

  • #3
    aku
    aku is offline
    New Coder
    Join Date
    Jan 2007
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <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 ?

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    Adirondacks
    Posts
    516
    Thanks
    4
    Thanked 4 Times in 4 Posts
    you made em' equal on the previous line though

  • #5
    aku
    aku is offline
    New Coder
    Join Date
    Jan 2007
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 ?

  • #6
    Regular Coder
    Join Date
    Jun 2002
    Location
    Adirondacks
    Posts
    516
    Thanks
    4
    Thanked 4 Times in 4 Posts
    lemme see the pop function referenced please

  • #7
    Regular Coder
    Join Date
    Jun 2002
    Location
    Adirondacks
    Posts
    516
    Thanks
    4
    Thanked 4 Times in 4 Posts
    scratch that, I know what ya mean
    http://www.w3schools.com/jsref/jsref_pop.asp

    you want to use shift not pop

  • #8
    aku
    aku is offline
    New Coder
    Join Date
    Jan 2007
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    no thanks, i dont want use shift ( shift discard first element ).

  • #9
    Regular Coder
    Join Date
    Jun 2002
    Location
    Adirondacks
    Posts
    516
    Thanks
    4
    Thanked 4 Times in 4 Posts
    Quote Originally Posted by aku View Post
    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.

  • #10
    aku
    aku is offline
    New Coder
    Join Date
    Jan 2007
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok
    read here and MAYBE you'll understand what i mean...

  • #11
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

    Edit:
    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.
    Last edited by david_kw; 02-08-2007 at 09:56 PM. Reason: see edit note

  • #12
    aku
    aku is offline
    New Coder
    Join Date
    Jan 2007
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yeah this is a nice answer!! thanks david


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •