0

我正在尝试构建一个对象数组,然后根据对象是否不存在更新对象或创建一个新对象。这是我第一次尝试使用 $.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;
}
4

2 回答 2

1
var newphase = Phase(item.Phase, itemhtml, 1)

应该

var newphase = new Phase(item.Phase, itemhtml, 1)
于 2013-04-19T12:36:36.303 回答
1

您需要使用new关键字来创建对象:

var newphase = new Phase(item.Phase, itemhtml, 1);
于 2013-04-19T12:37:04.300 回答