0

I need to merge two javascript object and set it to my global object. I'm using "Object.assign" method but it doesn't work.It works right when it called first time. But I need to merge each time my new object to the global object. But it doesnt work more first time ,it makes my global just same as last object.Do you know any other way to make it ? Thanks in advance :/

Javscript codes :

    //global object
    Admin.bufferCreateJSON={};

    Admin.createBufferJSON=function(){
        var B_name=$("#B_name").val();
        var B_description=$("#B_description").val();
        var B_bufferType=$("#bufferTypeSelect").val();  

        var bufferData={};

        var common = {'name': B_name,'description': B_description};
        bufferData=Object.assign(bufferData, common);
        var bufferType={ 'bufferType[id]': B_bufferType}
        //following codes works right
        bufferData=Object.assign(bufferData, bufferType);

        //Admin.bufferCreateJSON is my global variable 
        // But when I want to merge it to my global variable it doesnt work
        Admin.bufferCreateJSON=Object.assign(Admin.bufferCreateJSON, bufferData);
        //shows me just last one 
        console.log(Admin.bufferCreateJSON);

    }
Resul Rzaeeff
  • 448
  • 4
  • 14
  • 30
  • It's not clear exactly what objects you want to merge, but `$.extend()` will do it for you. Although if it's all hardcoded as in your question, merging is pretty redundant – Rory McCrossan Apr 25 '17 at 06:32
  • [Check this Link1](https://gist.github.com/svlasov-gists/2383751) [Link2](https://plainjs.com/javascript/utilities/merge-two-javascript-objects-19/) Hope this helpful. – user688 Apr 25 '17 at 06:57

1 Answers1

0

let mergedObj = Object.assign({}, obj1, obj2, obj3); is the best way.

Snippet of your case:

var Admin = {
    bufferCreateJSON: {
        a: 123,
        b: 321,
    }
}

Admin.createBufferJSON=function(tmp){
    var B_name = 'B_name' + tmp; //$("#B_name").val();
    var B_description = 'B_description' + tmp; //$("#B_description").val();
    var B_bufferType = 'B_bufferType' + tmp; //$("#bufferTypeSelect").val();  

    var common = {'name': B_name,'description': B_description};
    var bufferData=Object.assign({}, common);
    var bufferType={ 'bufferType[id]': B_bufferType}
    bufferData=Object.assign({}, bufferData, bufferType);

    Admin.bufferCreateJSON=Object.assign({}, Admin.bufferCreateJSON, bufferData);
    console.log(Admin.bufferCreateJSON);
}

Admin.createBufferJSON(1)
Admin.createBufferJSON(2)

Console output:

Object {a: 123, b: 321, name: "B_name1", description: "B_description1", bufferType[id]: "B_bufferType1"}
Object {a: 123, b: 321, name: "B_name2", description: "B_description2", bufferType[id]: "B_bufferType2"}

Object contain a and b from globals object and name, description, bufferType[id] are replaced as expected.

if you want only to add new properties without replacement of exists properties you can change assign order:

Admin.bufferCreateJSON=Object.assign({}, bufferData, Admin.bufferCreateJSON);

h0x91B
  • 1,211
  • 15
  • 19
  • I wrote like this Admin.bufferCreateJSON= Object.assign({}, Admin.bufferCreateJSON, bufferData); instead of Admin.bufferCreateJSON=Object.assign(Admin.bufferCreateJSON, bufferData); . But still console.log show me last object :/ I need to merge bufferData to my global variable when I call my "createBufferJSON" method – Resul Rzaeeff Apr 25 '17 at 06:39
  • please show me `console.log('type: %s', typeof Admin.bufferCreateJSON, Admin.bufferCreateJSON)` – h0x91B Apr 25 '17 at 06:42
  • type: object Object {name: "asdasd", description: "asdasd", bufferType[id]: "41802"} – Resul Rzaeeff Apr 25 '17 at 06:44
  • Edited the answer, compare your code line by line with it. I pretty sure that you just forgot somewhere `Object.assign({},...)` (empty object {}) – h0x91B Apr 25 '17 at 07:01
  • But I dont need to merge bufferData to empty object , I nedd to merge to my existing global variable :/ – Resul Rzaeeff Apr 25 '17 at 07:03
  • Thank you it works when i call it ;) But when my method called second time Admin.bufferCreateJSON again just show me last object. But i nedd to assign to its last version. – Resul Rzaeeff Apr 25 '17 at 07:11
  • Sorry, morning, my head does not work well. There is no deep extend and Object.assign do replace of exists properties. Check snippet now please. – h0x91B Apr 25 '17 at 07:39
  • No problem, thanks to all ;) – Resul Rzaeeff Apr 25 '17 at 07:40
  • New code is working for you? – h0x91B Apr 25 '17 at 08:03
  • I have solved the problem. I make my global Admin.bufferCreateJSON array Admin.bufferCreateJSON=[]; and push every object to it . Admin.bufferCreateJSON.push(bufferData); – Resul Rzaeeff Apr 25 '17 at 08:10