Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to add values to an array with overriding

I am trying to add values into an array but I have noticed that array is only updating the latest value.

Here is my JSON response:

   {
      "NA":{
         "Date":"2023-06-19",
         "Time":"13:01:00",
         "Timezone":"PST"
      }
   },
   {
      "EU":{
         "Date":"2023-06-19",
         "Time":"13:01:00",
         "Timezone":"GMT"
      }
   },
   {
      "IN":{
         "Date":"2023-06-19",
         "Time":"13:01:00",
         "Timezone":"IST"
      }
   }
]

Whats happening is that when I insert the values into the array, it only adds the latest value to but ignores everything else so if there is only NA it will display NA but if there is NA, EU and IN it will only display IN. How would I go about having all the values show in the array?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

      var regionsData = JSON.parse(element[0]["regions"]);
      regionsData.forEach((value) => {
        dataObj.regions = [
          Object.keys(value) +
            " " +
            value[Object.keys(value)[0]].Date +
            " " +
            value[Object.keys(value)[0]].Time +
            " " +
            value[Object.keys(value)[0]].Timezone,
        ];
      });

element[0]["regions"] is the JSON response I listed, I am parsing it as it is being return as a string.

    dataInfo.forEach((element) => {
      var dataObj = {
        audit_reference_id: element[0]["audit_reference_id"],
        subject: element[0]["subject"],
        leader_alias: element[0]["leader_alias"],
        exclude_user: element[0]["exclude_user"],
        regions: element[0]["regions"],
        exclusion_days: element[0]["exclusion_days"],
        escalation: element[0]["escalation"],
        job_family: element[0]["job_family"],
        creation_date: element[0]["creation_date"],
        created_by: element[0]["created_by"],
        updated_date: element[0]["updated_date"],
        updated_by: element[0]["updated_by"],
      };

>Solution :

In the forEach loop you are setting dataObj.regions equal to a new array. So dataObj.regions is only going to reflect the last value you set it to.

This should work instead:

const newRegions = [];
regionsData.forEach((value) => {
  newRegions.push( /* ... something ... */ );
});
dataObj.regions = newRegions;

you can also simplify this to

dataObj.regions = regionsData.map((value) => {
  return { /* ... some new object ... */ };
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading