0

I have two objects such as:

{ Count: 1,
  Items:
   [ { foo: [Object],
       name: [Object],
       bar: [Object],
       baz: [Object],
       qux: [Object] } ] }

and

{ Count: 0, Items: [] }

I need to combine them and return one JSON object. However, when I try this, I get

"[object Object][object Object]"

code:

function returnResponse(obj1, obj2) {
            res.statusCode = 200;
            res.setHeader('Content-Type', 'text/plain; charset=UTF-8');
            var returnResult = obj1 + obj2
            res.send(JSON.stringify(returnResult, undefined, 2));
            res.end();
        }

How do I get all the objects to appear correctly in the browser?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Cmag
  • 14,946
  • 25
  • 89
  • 140
  • 3
    How do you expect the "combined" result to look like? `+` performance string concatenation and the default string representation of an object is `[object Object]`. – Felix Kling Dec 09 '13 at 01:03
  • 3
    FYI, it seems you are confusing JavaScript object literals (constructs of the JavaScript language syntax) with JSON (a language-independent data-exchange format, like XML or CSV). `obj1` and `obj2` are objects, not JSON objects. [There is no such thing as a "JSON object"](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/). – Felix Kling Dec 09 '13 at 01:06
  • I think this makes no difference. He certainly wrote about plain JS objects. – Bohdan Yurov Dec 09 '13 at 01:08
  • In the end, im trying to return JSON objects to the browser – Cmag Dec 09 '13 at 01:09
  • 1
    @nick4fake: Being able to use terminology is very important when communicating with others. It might be clear from the context that the OP is talking about JS objects, but if I can educating them now then they won't make the same mistake in a different, maybe less obvious setting. – Felix Kling Dec 09 '13 at 01:10
  • @Clustermagnet: Yes, I understand that. But you only generate JSON in the end, as the result, the objects/data you are actually working with is not JSON and hence the problem isn't really related to JSON at all. – Felix Kling Dec 09 '13 at 01:11
  • Also, you still haven't explained what you expect as result. As you can see, answers range from creating arrays to merging objects. If you explain what you expect we can help you better. – Felix Kling Dec 09 '13 at 01:15
  • @FelixKling the result i would like to look like a single JSON object in the browser – Cmag Dec 09 '13 at 01:17
  • So you **don't** want an array of two objects (`[ {Count: 1, ...}, {Count: 0, ...}]`)? You want to merge the properties of both objects, and overwrite the properties in the first objects with the one in the second objects? The result would be `{ Count: 0, Items: [] }` then. Or do you want to recursively merge the properties? – Felix Kling Dec 09 '13 at 01:18
  • 1
    @Clustermagnet: Here is a single JS object (please stop saying JSON object): `{}`. Here is another: `{ combined: true }`. There is near-infinite "single objects". These are obviously not what you want, but it is much less obvious to us what you *do* want. Please edit your question and explicitly write out what you expect the result of the "combination" of your two example objects is. – Amadan Dec 09 '13 at 01:18
  • 3
    Based in the accepted answer it looks like you wanted an array of objects. If you had posted an example of what you expected the output to be, this would have been much simpler. – Felix Kling Dec 09 '13 at 01:21

3 Answers3

2

I think you're looking to return both objects as an array:

function returnResponse(obj1, obj2) {
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/plain; charset=UTF-8');
    var returnResult = [obj1,obj2];
    res.send(JSON.stringify(returnResult, undefined, 2));
    res.end();
}
AlliterativeAlice
  • 11,841
  • 9
  • 52
  • 69
1

If you don't use JS framework like jQuery, you need an recursive merge function. Take a look at this one for example.

Community
  • 1
  • 1
Maciej Sz
  • 11,151
  • 7
  • 40
  • 56
0

You may use $.extend method of jQuery library.

Read more: http://api.jquery.com/jQuery.extend/

Without jQuery, one-liner:

for (var attrname in obj2) { obj1[attrname] = obj2[attrname]; }

Also, this is a duplicate: How can I merge properties of two JavaScript objects dynamically?

Community
  • 1
  • 1
Bohdan Yurov
  • 365
  • 5
  • 9
  • 2
    The OP doesn't seem to use jQuery. If it's a duplicate, then flag/vote to close it as such. *edit:* you did. – Felix Kling Dec 09 '13 at 01:04
  • I wrote, that this can be achieved with jQuery, but I did not write that was the only way. Also I marked the question as duplicate and given the link to OP. – Bohdan Yurov Dec 09 '13 at 01:06
  • Im not using jquery, this is a node app – Cmag Dec 09 '13 at 01:06
  • Please, read my answer again. There is a link to the same question answered before. – Bohdan Yurov Dec 09 '13 at 01:07
  • 1
    In general, you should only propose solutions using a certain library, if the library is mentioned in the question or listed in the tags. – Felix Kling Dec 09 '13 at 01:07
  • This is not a good place for such discussion, but I'll take this into account. – Bohdan Yurov Dec 09 '13 at 01:10
  • 1
    @FelixKling In general, when you present rules like "In general, you should only propose solutions using a certain library, if the library is mentioned in the question or listed in the tags" you should provide a link to the rule. – Louis Dec 09 '13 at 01:12
  • 1
    @Louis: Sorry, I cannot provide a link. But if you hover over the [tag:javascript] tag, you find the following in the tag description: *"Unless a tag for a framework/library is also included, a pure JavaScript answer is expected."* – Felix Kling Dec 09 '13 at 01:14
  • @FelixKling: or OP forgot to add a tag. I think, we may discuss this in chat, rather posting comments to an anwser. – Bohdan Yurov Dec 09 '13 at 01:15