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"
}]
Timigen
  • 1,055
  • 1
  • 17
  • 33
vengatesh rkv
  • 327
  • 2
  • 16
  • Your string1 is not string but an array and string2 is object. Are they actual string you mean ? – Vigneswaran Marimuthu Apr 27 '15 at 12:20
  • possible duplicate of [Merge two json/javascript objects in to one object](http://stackoverflow.com/questions/10384845/merge-two-json-javascript-objects-in-to-one-object) – DSF Apr 27 '15 at 12:21
  • You should have a look at [data-types](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures) in javascript. Your `strings` are `objects` and the expected output is an `array` containing an `object` – empiric Apr 27 '15 at 12:49

2 Answers2

6

You can use the jQuery.extend method:

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

Demo

empiric
  • 7,825
  • 7
  • 37
  • 48
adamc-au
  • 215
  • 1
  • 9
1

Use this

string1[0] = $.extend({}, string1[0], string2);
vasilenicusor
  • 2,023
  • 1
  • 21
  • 37