2

Object 1:

var string1 = {
    "jobroleid": "1",
    "technologyid": "1",
    "jobrole": "SOFTWARE DEVELOPER",
    "technology": "DOTNET",
    "yoc": [],
    "degree": [],
    "gender": ["Female"],
    "credit": [],
    "minqp": "6",
    "maxqp": "7"
};

Object 2:

var string2={'name':'hai'};

How can I merge the two objects?

Expected output:

[{
    "jobroleid": "1",
    "technologyid": "1",
    "jobrole": "SOFTWARE DEVELOPER",
    "technology": "DOTNET",
    "yoc": [],
    "degree": [],
    "gender": ["Female"],
    "credit": [],
    "minqp": "6",
    "maxqp": "7",
    "name": "hai"
}]
3
  • Your string1 is not string but an array and string2 is object. Are they actual string you mean ? Commented Apr 27, 2015 at 12:20
  • possible duplicate of Merge two json/javascript objects in to one object Commented Apr 27, 2015 at 12:21
  • You should have a look at data-types in javascript. Your strings are objects and the expected output is an array containing an object Commented Apr 27, 2015 at 12:49

2 Answers 2

6

You can use the jQuery.extend method:

var combined = []; 
combined.push($.extend({},string1,string2));

Demo

Sign up to request clarification or add additional context in comments.

1 Comment

without using {}, i got a answer <code>$.extend({},string1,string2);</code>
1

Use this

string1[0] = $.extend({}, string1[0], string2);

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.