0

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;
}
2
  • What does it do or what doesn't it do? What is going wrong? Commented Apr 19, 2013 at 12:35
  • Can you create a demo at jsfiddle Commented Apr 19, 2013 at 12:37

2 Answers 2

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

should be

var newphase = new Phase(item.Phase, itemhtml, 1)
Sign up to request clarification or add additional context in comments.

1 Comment

yep, stupid mistake... sometimes it just takes another set of eyes i guess.
1

You need to use the new keyword to create an object:

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

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.