-3

Using the code below I'm trying to concat two objects, but I am getting an error: Uncaught TypeError: undefined is not a function

Here is my code:

function getInvitees(pid, o, u)
{
    var invitees = {};
    var inviteCount = 0;

do {
    var inviteeURL = "https://www.****.com/a/invitees/?pid={0}&offset={1}&user={2}".format(pid, o, u);

    sendRequest(friendsURL, function() { 
        var resp = JSON.parse(e.responseText);
        tmp_invitees = resp['invitees'];
        invitees = invitees.concat(tmp_invitees);
        inviteeCount = tmp_invitees.length;
    });
} while(inviteeCount != 0);

return invitees;

}

Why am I unable to contact invitees with tmp_invitees? I'm able to access invitees it just appears invitees doesn't have a concat() method.

James Jeffery
  • 12,093
  • 19
  • 74
  • 108

1 Answers1

-1

Because invitees is a dictionary (or json-object, or JS-Object) and not an array. You can't concat 2 different objects of a different type.

  • Oh. I'm confused because `resp['invitees']` contains the JSON object. I thought you could concat a JSON object with another JSON object? – James Jeffery Sep 08 '14 at 19:50
  • No only Array objects, not default objects (thus JSON styled objects). You would need to merge those with other means. Try googling for it or search the MDN for dictionary functions) –  Sep 08 '14 at 19:50
  • I got the answer from here: http://stackoverflow.com/questions/10384845/merge-two-json-objects-in-to-one-object ... which is the same as my code above. How are they able to concat 2 objects? – James Jeffery Sep 08 '14 at 19:51
  • Because both JSON objects are wrapped inside an Array object `[]`. So what happens there is that the 2 Array's their first key (`0`) get joined and concatted. Thus resulting in a new combined entry. Beware tho. What Quentin answers in that question is not a merge of 2 JSON objects. It's a merge of 2 Array Keys into 1 Array with 2 JSON objects ;) (`[ {} , {} ]`) –  Sep 08 '14 at 19:55
  • Ah! Got it. I see where I'm going wrong. Going to vote because you helped me understand the problem and solve it. – James Jeffery Sep 08 '14 at 20:00