我正在尝试构建一个对象数组,然后根据对象是否不存在更新对象或创建一个新对象。这是我第一次尝试使用 $.grep 函数,我不确定我做错了什么。有什么建议吗?
以下是对象:
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;
}
这是有问题的代码....
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;
}