I come back to this fabulous site to expose a behavior that I don't understand.
I create a json dynamically. I gradually add objects in a array. For that, I add firstly a "template". Then, in a second time, I modify the value of the object. But, this updating has been applied to all elements of the array that have been created with the same template. Whereas I expected that the updating modified only one object. I reproduced the problem to its minimum below:
thanks in advance
<!DOCTYPE html>
<html>
<head>
<title>Layers Control Tutorial - Leaflet</title>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
.fa-test {
width: 24px;
height: 24px;
background-repeat: no-repeat;
background-image: url(data:image/svg+xml;base64,PHN2ZyBhcmlhLWhpZGRlbj0idHJ1ZSIgZm9jdXNhYmxlPSJmYWxzZSIgZGF0YS1wcmVmaXg9ImZhcyIgZGF0YS1pY29uPSJmaWxlLWRvd25sb2FkIiBjbGFzcz0ic3ZnLWlubGluZS0tZmEgZmEtZmlsZS1kb3dubG9hZCBmYS13LTEyIiByb2xlPSJpbWciIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDM4NCA1MTIiPjxwYXRoIGZpbGw9ImN1cnJlbnRDb2xvciIgZD0iTTIyNCAxMzZWMEgyNEMxMC43IDAgMCAxMC43IDAgMjR2NDY0YzAgMTMuMyAxMC43IDI0IDI0IDI0aDMzNmMxMy4zIDAgMjQtMTAuNyAyNC0yNFYxNjBIMjQ4Yy0xMy4yIDAtMjQtMTAuOC0yNC0yNHptNzYuNDUgMjExLjM2bC05Ni40MiA5NS43Yy02LjY1IDYuNjEtMTcuMzkgNi42MS0yNC4wNCAwbC05Ni40Mi05NS43QzczLjQyIDMzNy4yOSA4MC41NCAzMjAgOTQuODIgMzIwSDE2MHYtODBjMC04Ljg0IDcuMTYtMTYgMTYtMTZoMzJjOC44NCAwIDE2IDcuMTYgMTYgMTZ2ODBoNjUuMThjMTQuMjggMCAyMS40IDE3LjI5IDExLjI3IDI3LjM2ek0zNzcgMTA1TDI3OS4xIDdjLTQuNS00LjUtMTAuNi03LTE3LTdIMjU2djEyOGgxMjh2LTYuMWMwLTYuMy0yLjUtMTIuNC03LTE2Ljl6Ij48L3BhdGg+PC9zdmc+);
}
</style>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script>
var featureLayerJsonForRequest = {
"CreateFeatureLayerRequest":{
"themeId": "PTV_TruckAttributes",
"features": [
{
"segments":[
{
"startNode": "string",
"endNode": "string",
"direction": "BOTH"
}
],
"descriptions":[
{
"attributes":[
{
"key": "string",
"value": "string"
}
],
"timeDomain": "string"
}
]
}
]
}
};
var templateDescription = {"attributes":[
{
"key": "string",
"value": "string"
}
],
"timeDomain": "new"
};
// Before
console.log( featureLayerJsonForRequest.CreateFeatureLayerRequest.features[0].descriptions)
// Add a second description in the Json (with multiple way)
//featureLayerJsonForRequest.CreateFeatureLayerRequest.features[0].descriptions.push(featureLayerJsonForRequest.CreateFeatureLayerRequest.features[0].descriptions[0]) ;
featureLayerJsonForRequest.CreateFeatureLayerRequest.features[0].descriptions.push(templateDescription) ;
//featureLayerJsonForRequest.CreateFeatureLayerRequest.features[0].descriptions[size] = templateDescription;
//featureLayerJsonForRequest.CreateFeatureLayerRequest.features[0].descriptions[size] = templateDescription;
// Add a third description in the json
featureLayerJsonForRequest.CreateFeatureLayerRequest.features[0].descriptions.push(templateDescription);
//featureLayerJsonForRequest.CreateFeatureLayerRequest.features[0].descriptions.push( featureLayerJsonForRequest.CreateFeatureLayerRequest.features[0].descriptions[0]); // I would like do this
// Update the third element
featureLayerJsonForRequest.CreateFeatureLayerRequest.features[0].descriptions[2].timeDomain = "Updating value";
//After
console.log( featureLayerJsonForRequest.CreateFeatureLayerRequest.features[0].descriptions) // the second and the third element have the same value for the timeDomain Key
</script>
</body>