jquery - Merge 2 sets of Object + Array Using Javascript -


i trying merge 2 sets of object & array below

{   exclude: ['placeholder'],   attributes: { myid : '' }   map: 'input' } 

and

{  exclude: ['options'],  attributes: { class: '', id:'', name:'', },  map: '', } 

i tried using jquery's $.extend function worked merge objects. , tried lodash js dose not work. output

{   exclude: ['options'],   attributes: { class: '', id: '', name: '', myid: '' },   map: '', } 

i need output like

{   exclude: ['options','placeholder'],   attributes: { class: '', id: '', name: '', myid: '' },   map: '', } 

code tried using

$.extend(true,{   exclude: ['placeholder'],   attributes: { myid : '' }   map: 'input' },{  exclude: ['options'],  attributes: { class: '', id:'', name:'', },  map: '', }); 

you can create custom function using for...in loop , check type of each value.

var obj1 = {    exclude: ['placeholder'],    attributes: { myid : '' },    map: 'input'  }    var obj2 = {   exclude: ['options'],   attributes: { class: '', id:'', name:'', },   map: '',  }      function merge(o1, o2) {    (var in o2) {      if (o1[i]) {        if (typeof o2[i] == 'object') {          if (array.isarray(o2[i])) o1[i].push(...o2[i])          else o1[i] = object.assign({}, o1[i], o2[i])        } else {        	 o1[i] = o2[i]        }      } else {        o1[i] = o2[i]      }    }    return o1;  }      console.log(merge(obj1, obj2))


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -