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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    May 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Array in javascript

    when i copied a array to another variable and sorted the one, but its affecting the original array variable


    function srt1 ()
    {
    arr2=arr1;
    var srt=arr1.sort();
    document.main.srt.value=srt;
    }

    where
    arr1={9,8,3,6,7,1,4}
    arr2={9,8,3,6,7,1,4}
    after sorting
    arr1={1,3,4,6,7,8,9}
    arr2={1,3,4,6,7,8,9}

    please give a answer, in advance Thank you

  • #2
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    945
    Thanks
    0
    Thanked 129 Times in 128 Posts
    i can't tell you why it is made so all copies are affected, but I can give you a workaround.

    srt= arr1.slice(0); makes srt become the result of slicing 0 elements from arr1 without being a direct copy of it.

    This array can then be sorted without affecting the original array, nor direct copies of the original array.
    srt.sort();

    you can merge these to action in to a single command.
    Code:
    srt= arr1.slice(0).sort();

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,022
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Assigning an array or an object to a variable makes a reference to the value.

    Code:
    <script type = "text/javascript">
    
    function srt1() {
    var arr1=[9,8,3,6,7,1,4];
    var arr2 = arr1.slice(0);  // copy the values to arr2
    arr1.sort();
    alert (arr1);  // 1,3,4,6,7,8,9
    alert (arr2); // 9,8,3,6,7,1,4
    }
    
    </script>
    Objects are trickier because there is no native method which returns a copy of the object. For more info see http://my.opera.com/GreyWyvern/blog/show.dml/1725165

    He [John Terry] wears his shirt on his sleeve. - Commentator Sky Sports News

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,296
    Thanks
    13
    Thanked 345 Times in 341 Posts
    Quote Originally Posted by Philip M View Post
    Objects are trickier because there is no native method which returns a copy of the object. For more info see http://my.opera.com/GreyWyvern/blog/show.dml/1725165
    note: it was not explicitly mentioned in the Article, but never try to copy (or deep-clone) DOM objects (they contain circular references that would get you stuck in infinite recursion)
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

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