0

When I try to push the temp object of type update it creates duplicates instead of adding a new object to updatedjson. Basically I am reading list of ip address from a file and create new json objects of type sample and adding it to updatedjson array.

 sample =[
   {
    "destination": "string",
    "destinationType": "string",
    "isStateless": true,
    "protocol": 6,
    "tcpOptions": {
      "destinationPortRange": {
        "max": 0,
        "min": 0
      }
    }
  }
];
var updatedjson = [];
var tempjson = sample[0];
tempjson.isStateless = false;
tempjson.protocol = 6;
tempjson.tcpOptions.destinationPortRange.min = 443;
tempjson.tcpOptions.destinationPortRange.max = 443;
tempjson.destinationType = "CIDR_BLOCK";

for (var i = 0; i < inputArray.length; i++) {
  for (var j = 0; j < inputArray[i].cidrs.length; j++) {
    var tempjson = sample[0];
    tempjson.isStateless = false;
    tempjson.protocol = 6;
    tempjson.tcpOptions.destinationPortRange.min = 443;
    tempjson.tcpOptions.destinationPortRange.max = 443;
    tempjson.destinationType = "CIDR_BLOCK";

    tempjson.destination = inputArray[i].cidrs[j];
 
    addToUpdate(tempjson);
  }
}

function addToUpdate(temp) {
    updatedjson.push(temp);
}

Even if the tempjson.destination is different it pushes same object like for ip address 166.35.25.12/24 and 36.34.25.12/24 updatedjson is like this.

[
  {
    destination: '166.35.25.12/24',
    destinationType: 'CIDR_BLOCK',
    isStateless: false,
    protocol: 6,
    tcpOptions: { destinationPortRange: [Object] }
  },
  {
    destination: '166.35.25.12/24',
    destinationType: 'CIDR_BLOCK',
    isStateless: false,
    protocol: 6,
    tcpOptions: { destinationPortRange: [Object] }
  }
]
Darsh
  • 21
  • 4
  • The answer and the duplicate explain the issue. The simplest fix would be to change `updatedjson.push(temp);` to `updatedjson.push({...temp});`. This only works well because `destination` is at the root. But you could always use a more sophisticated `clone` functions if needed. – Scott Sauyet Aug 20 '20 at 18:09
  • Thanks @ScottSauyet, that solved the issue. – Darsh Aug 20 '20 at 18:15
  • @ScottSauyet `{...temp}` is a shallow copy. If the OP does something like `temp.tcpOptions.destinationPortRange.min = 420` the change will reflect in every element in the array. – Code-Apprentice Aug 20 '20 at 20:06
  • 1
    @Code-Apprentice: That's what I tried to explain by "`destination` is at the root". But yes, for anything more complex, we would need a more sophisticated `clone` function, even if it's just `o => JSON.parse (JSON.stringify (o))`. – Scott Sauyet Aug 21 '20 at 12:29

1 Answers1

0

When I try to push the temp object of type update it creates duplicates instead of adding a new object to updatedjson

This is because you keep pushing the same object over and over in the loop. If you want to push a new object, you must create a new object each time in the loop. Remember JavaScript does exactly what you tell it to. No more, no less.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268