i can't copy the array

This question already has an answer here:

  • Is JavaScript a pass-by-reference or pass-by-value language? 29 answers
  • (Deep) copying an array using jQuery [duplicate] 8 answers

  • Arrays are objects, unlike the primitive types like string, int, etc ... variables that take objects correspond to references (pointer) for objects, rather than the object itself, so different variables can reference the same object. Variable of primitive type (string, int, etc.) are associated to values​​.

    In your case you will have to clone your object array for have the same values​​.

    var Mycollection = new Array("James","Jonh","Mary");
    var Mycollection2 = Mycollection.slice();
    

    JavaScript通过引用传递数组,以使单独的数组执行:

    var Mycollection = new Array("James","Jonh","Mary");
    var Mycollection2 = Mycollection.slice();
    

    just use:

    var Mycollection2 = Mycollection.slice(0);

    to copy the array

    链接地址: http://www.djcxy.com/p/20810.html

    上一篇: 即时通讯困惑与JavaScript数组工作

    下一篇: 我无法复制数组