When attempting to insert an object into a JSON object, I am encountering an issue where it duplicates the JSON object. Here is a breakdown of the scenario:
<input type="text" style="width: 40% !important;" placeholder="Nom" class="input-sm" ng-model="company.link.nom" />
<input type="text" style="width: 40% !important;" placeholder="Lien" class="input-sm" ng-model="company.link.value" />
<a class="btn btn-wide btn-primary" ng-click="company.addExternalLinktoGrid()"><i class="fa fa-plus"></i> Add </a>
The function addExternalLinktoGrid
handles this process:
var linkJsonObj = [];
var cp=1;
company.addExternalLinktoGrid = function() {
company.link.id=cp;
currentObj.push(company.link);
console.log(JSON.stringify(company.link));
linkJsonObj.push(company.link);
console.log(JSON.stringify(linkJsonObj));
cp++;
}
For instance, if we add a new object with values company.link.nom="toto"
and company.link.value="titi"
,linkJsonObj
will display:
[{"nom":"toto","value":"titi","id":1}]
If we then add another object with values company.link.nom="momo"
and company.link.value="mimi"
, linkJsonObj
ends up displaying:
[{"nom":"momo","value":"mimi","id":2},
{"nom":"momo","value":"mimi","id":2}]
This unexpected duplication occurs. The intended output should be:
[{"nom":"toto","value":"titi","id":1},
{"nom":"momo","value":"mimi","id":2}]
If anyone has insight or assistance, it would be greatly appreciated.