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?

11
  • 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]. Commented Dec 9, 2013 at 1: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". Commented Dec 9, 2013 at 1:06
  • 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. Commented Dec 9, 2013 at 1:10
  • 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. Commented Dec 9, 2013 at 1: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. Commented Dec 9, 2013 at 1:21

3 Answers 3

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();
}
Sign up to request clarification or add additional context in comments.

1 Comment

I wonder why this was downvoted :-/
1

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

2 Comments

This a comment, not an answer.
If you think that an existing answer perfectly solves this question, you should flag/vote to close this question as duplicate.
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?

9 Comments

The OP doesn't seem to use jQuery. If it's a duplicate, then flag/vote to close it as such. edit: you did.
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.
In general, you should only propose solutions using a certain library, if the library is mentioned in the question or listed in the tags.
@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: Sorry, I cannot provide a link. But if you hover over the 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."
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.