我正在尝试构建一个对象数组,然后更新对象或创建一个新对象,具体取决于是否存在。这是我第一次尝试使用$ .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;
}
答案 0 :(得分:1)
var newphase = Phase(item.Phase, itemhtml, 1)
应该是
var newphase = new Phase(item.Phase, itemhtml, 1)
答案 1 :(得分:1)
您需要使用new
关键字来创建对象:
var newphase = new Phase(item.Phase, itemhtml, 1);