2

I am getting an unexpected identifier error for the following function.

function merge (one, two) {
    one.forEach(function(assign){

     //////////this next line is throwing the error////////////
        if (two.some(function(req) req.related == assign.rid)) {
            if (one.some(function(iter) iter.rid == req.rid)) {
                iter.quantity++;
            } else {
                one.push(req);
            }
        }
    });

    return one; 
}   

The function is intended to operate on an array of objects.

4
  • I'm 2/3ds sure you have to use braces around the anonymous function body, and that that confuses the parser into trying to evaluate req: function(req) { req.related == assign.rid } Commented Dec 21, 2015 at 7:01
  • 1
    To make your questions easier to answer well, you should consider making a Minimal, Complete, Verifiable example Commented Dec 21, 2015 at 7:02
  • @millimoose You're right, I was afraid if I omitted any code it would remove necessary information. I'm new enough to javascript to make that mistake. Commented Dec 21, 2015 at 7:05
  • Well, I also had in mind adding some code where merge() is called with example inputs. It's not obvious what would be valid input here beyond "arrays." But generally, the process of removing code, while retesting to make sure the error still crops up will, in cases like these, usually lets you find the error on your own in the first place. Kind of like a doctor poking a patient all over to figure out where it hurts. Commented Dec 21, 2015 at 7:34

6 Answers 6

4

You have missed some { } around .some(function()...

function merge (one, two) {
    one.forEach(function(assign){

        if (two.some(function(req){ req.related == assign.rid})) {
                               // ^-- This one you missed
            if (one.some(function(iter){ iter.rid == req.rid})) {
                                    // ^-- This one you missed as well
                iter.quantity++;
            } else {
                one.push(req);
            }
        }
    });

    return one; 
}  
Sign up to request clarification or add additional context in comments.

Comments

1

Check out the some function call. It certainly is a function being passed as parameters, so curly brackets are necessary.

Comments

1

.some() expects function as the parameter.
If you want to pass anonymous function, then use curly braces:

if (two.some(function(req) { return req.related == assign.rid; })) 
{
    if (one.some(function(iter) { return iter.rid == req.rid; })) 
    {
        iter.quantity++;
    } else {
        one.push(req);
    }
}

Comments

1

you are missing open/close braces {} inside function

writing a function should be like this:

$(function(){
//code
})

Comments

1

The some function needs curly braces/(paranthesis) to be opened and closed, please read this link, this is a good explanation on the function.

Comments

1

You aren't calling your functions properly. You need brackets after defining the function's arguments.

For example this is what two.some should look like:

two.some(function(req){ req.related == assign.rid })

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.