2

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}
             ];

4 Answers 4

6

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);
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

You could just use filter and find that object.

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
}];

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

function findOneByMonthAndYear(arr, month, year) {
    return arr.filter(function(item) {
        return item.year === year && item.month === month;
    })[0];
}

var item = findOneByMonthAndYear(arrayObj, newData.month, newData.year);
item.value = newData.value;

console.log(arrayObj);

Comments

1

Just select the cell from the array you want to update like so.

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

 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}
             ];

for (var z = 0; z < arrayObj.length; z++) {
   if(arrayObj[z].year="2015" && arrayObj[z].month="FEB")
        arrayObj[z].value=n.value;
}

Ofcourse,you can make it more dynamic as per your needs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.