Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript: Dynamically add new value to update JSON/ object

I have dynamically created object(predefined) and set value to 0 for all months(actually 12 months I have shown only 3 months) for all years(2 years, 2015 and 2014). So my data grid will look all months with 0 value. see below code

 var arrayObj = [
               {"year" : 2015, "month" : "JAN", "value" : 0},
               {"year" : 2015, "month" : "FEB", "value" : 0},
               {"year" : 2015, "month" : "MAR", "value" : 0},
               {"year" : 2014, "month" : "JAN", "value" : 0},
               {"year" : 2014, "month" : "FEB", "value" : 0},
               {"year" : 2014, "month" : "MAR", "value" : 0}
             ];

Periodically, I will keep getting value of single month sometime in later in object format after form submit.

   eq. {"year" : 2015, "month" : "FEB", "value" : 2.33}

So my original object should also change accordingly, with javascript code. like below..

  var arrayObj = [
               {"year" : 2015, "month" : "JAN", "value" : 0},
               {"year" : 2015, "month" : "FEB", "value" : 2.33},
               {"year" : 2015, "month" : "MAR", "value" : 0},
               {"year" : 2014, "month" : "JAN", "value" : 0},
               {"year" : 2014, "month" : "FEB", "value" : 0},
               {"year" : 2014, "month" : "MAR", "value" : 0}
             ];
like image 668
sag Surya Avatar asked Jun 23 '26 01:06

sag Surya


2 Answers

I think map function would be ideal in this case. Please see the code segment below:

var src = [
           {"year" : 2015, "month" : "JAN", "value" : 0},
           {"year" : 2015, "month" : "FEB", "value" : 0},
           {"year" : 2015, "month" : "MAR", "value" : 0},
           {"year" : 2014, "month" : "JAN", "value" : 0},
           {"year" : 2014, "month" : "FEB", "value" : 0},
           {"year" : 2014, "month" : "MAR", "value" : 0}
         ];

var newRecord = {
"year": 2015,
"month": "FEB",
"value": 2.33
};

function updateJSON(src, newRecord) {
return src.map(function(item) {
  return (item.year === newRecord.year && item.month === newRecord.month) ? newRecord : item;
});
}
src = updateJSON(src, newRecord);
console.log(src);
like image 58
Beroza Paul Avatar answered Jun 25 '26 14:06

Beroza Paul


Walk the array, find the relevant month, and update its value:

var newData = { "year" : 2015, "month" : "FEB", "value" : 2.33 };

for (var i = 0; i < data.length; i++) {
    var entry = data[i];

    if (entry.year == newData.year && entry.month == newData.month)
        entry.value = newData.value;
}

Your structure is not ideal for this kind of update, however. If you had 10.000 entries, you may need to examine all of them to update a single one.

You should instead organize data by year and month. For example:

var data = {
    '2015-FEB': 2.5
    '2015-MAR': 3.8
};

Updating this other structure is a single operation:

data[year + '-' + month] = value;

If you need your objects in the form you posted, you can have the best of both worlds:

var data = {
    '2015-FEB': { "year" : 2015, "month" : "FEB", "value" : 2.33 }
};

If you need them in sorted order, you can have both structures: the date-to-object map and the array, storing references to the same objects.

like image 39
slezica Avatar answered Jun 25 '26 13:06

slezica



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!