Why does my original array get spliced if I splice the cloned array in JavaScript? -


i have following code:

 var coords = [      {lat: 39.57904, lng: -8.98094, type: "a"}, //      {lat: 39.55436, lng: -8.95493, type: "b"}, // b      {lat: 39.56634, lng: -8.95836, type: "c"} // c  ];   var travelingoptions = [];   getalltravelingoptions();   function getalltravelingoptions(){      coords.foreach((point, pos) => {          let c = coords;          delete c[pos];          console.log(c);          console.log(coords);      });  } 

why variable c , coords same? if delete on c, mirrors action on coords. normal behavior?

because of assignment of c, reference of array coords.

any change of coords effect c, until new value assigned c.

if make copy of array array.slice, new array same reference of objects. when changing 1 object inside, changing same object same reference in c.

var coords = [           {lat: 39.57904, lng: -8.98094, type: "a"}, //           {lat: 39.55436, lng: -8.95493, type: "b"}, // b           {lat: 39.56634, lng: -8.95836, type: "c"} // c       ],       c = coords.slice();    console.log(c);  coords[1].type = 'foo';  console.log(c);
.as-console-wrapper { max-height: 100% !important; top: 0; }


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -