I am trying to build an array of objects and then either update the object or create a new one depending if one does not exist. This is my first time trying to use the $.grep function and I am not sure what I am doing wrong. Any Advice?
Here are the objects:
function Phase(phase, html, count) {
this.phase = phase;
this.html = html;
this.count = count;
this.porfolios = [];
}
function Portfolio(portfolio, html, count) {
this.portfolio = portfolio;
this.html = html;
this.count = count;
}
Here is the code in question....
var phases = [];
//Check Array of Phases
result = $.grep(phases, function (e) { return e.phase == item.Phase; });
if (result.length == 0)
{
var newphase = Phase(item.Phase, itemhtml, 1)
phases.push(newphase);
}
else if (result.length == 1)
{
// update existing phase
result[0].count ++;
result[0].html += itemhtml;
}