-1

In this html/javascript code I want to reassign Q to A every time k changes (in the first loop). But I noticed that A change too!! so how do I fix the problem ?

  • why A changes its value although I am only changing Q ??
  • how do I prevent A from changing ?? thanks for answering

<!DOCTYPE html>
<html>
<body>


<p id="demo"></p>

<script>
var A=[[3,4],[5,7]];

var Q=[];

 for (var k=0;k<2;k++){
 
    Q= A;

    for (var i=0;i<2;i++) {
        Q[k][i]=1;
                    
       }
  }
document.getElementById("demo").innerHTML =  A;

</script>

</body>
</html>
XARAF
  • 3
  • 2
  • Because `Q = A` does **not** create a copy of the array, both `Q` and `A` references the same array object. – Lennholm Oct 21 '20 at 22:47
  • `Q= A` - you assigned a reference to `A` to `Q`, so when you changed `Q`, it changed `A` – Nick Oct 21 '20 at 22:47
  • When you assign A to Q, you set the reference to A so that both variables are assigned to the same array, but you need to clone the array like this `Q = [... A];` – Cristian Oct 21 '20 at 22:50
  • 1
    @Cristian `Q = [...A];` wouldnt work – Lawrence Cherone Oct 21 '20 at 22:51
  • @LawrenceCherone right, my example is for single level array, for nested you can use `Q = JSON.parse (JSON.stringify (A))` or `cloneDeep` from lodash. – Cristian Oct 21 '20 at 22:54
  • `var Q = A.map( item => new Array(item.length).fill(1));` would do the trick for what you are trying to do, but I guess this is just demo code, so you could do `var Q = JSON.parse(JSON.stringify(A))` to do a deep copy (provided there is no circular reference) – Icepickle Oct 21 '20 at 22:55
  • the json.parse/stringify trick would work but seems like a frowned upon bad practice or hack from partys which would suggest use lodashClonedeep etc – Lawrence Cherone Oct 21 '20 at 23:00
  • @LawrenceCherone doesn't that depend on the context, if you know what the array will contain, I see no reason why you would need to import an entire library for it. Certainly not based on this demo code – Icepickle Oct 21 '20 at 23:03
  • Its fine to use imo, im not one of those people that jump on a lib.. a one-liner `const deepClone = v => JSON.parse(JSON.stringify(v))` is fine.. `Q = deepClone(A);` but this is SO after all. and there is many ways to clone it, from looping over to recursive map, or Array.from etc all shown in the dupe answers – Lawrence Cherone Oct 21 '20 at 23:07

1 Answers1

-1

Try this. Reference is made here

<!DOCTYPE html>
<html>
<body>
  <p id="demo"></p>
  <script>
     function arrayClone( arr ) {
     
         var i, copy;
     
         if( Array.isArray( arr ) ) {
             copy = arr.slice( 0 );
             for( i = 0; i < copy.length; i++ ) {
                 copy[ i ] = arrayClone( copy[ i ] );
             }
             return copy;
         } else if( typeof arr === 'object' ) {
             throw 'Cannot clone array containing an object!';
         } else {
             return arr;
         }
     
     }
     
     A=[[3,4],[5,7]];
     var Q=arrayClone(A);
     
      for (var k=0;k<2;k++){
         for (var i=0;i<2;i++) {
             Q[k][i]=1;
                         
            }
       }
     
     document.getElementById("demo").innerHTML =  A;
     
  </script>
DarrenChand
  • 116
  • 13