How to merge irregular object












0














I wanna merge 2 objects.



One object is the default form.



let data = {
weight: '',
DOB: '',
gender: '',
}


The other is irregular like below that



let temp1 = {
weight: '19.2',
}

let temp2 = {
DOB: '1992-11',
}

let temp3 = {
DOB: '1992-11',
gender: 'Male',
}

let temp4 = undefined;


If I merge data and temp1, the result should be



let data = {
weight: '19.2',
DOB: '',
gender: ''
}


If I merge data and temp3, the result should be



let data = {
weight: '',
DOB: '1992-11',
gender: 'Male',
}


If I merge data and temp4, the result should be



let data = {
weight: '',
DOB: '',
gender: ''
}


Give me some advice to achieve it.



Thanks in advance.










share|improve this question






















  • Oh, you can't merge that way. You gotta set those values to undefined, right?
    – Praveen Kumar Purushothaman
    yesterday










  • If your default object always has the 3 keys, why not just check each different object for those properties? If they exist, take them, if not, ignore.
    – tomerpacific
    yesterday










  • @tomerpacific I can check there's existing or not, but I need to preserve the default object form.
    – zynkn
    yesterday
















0














I wanna merge 2 objects.



One object is the default form.



let data = {
weight: '',
DOB: '',
gender: '',
}


The other is irregular like below that



let temp1 = {
weight: '19.2',
}

let temp2 = {
DOB: '1992-11',
}

let temp3 = {
DOB: '1992-11',
gender: 'Male',
}

let temp4 = undefined;


If I merge data and temp1, the result should be



let data = {
weight: '19.2',
DOB: '',
gender: ''
}


If I merge data and temp3, the result should be



let data = {
weight: '',
DOB: '1992-11',
gender: 'Male',
}


If I merge data and temp4, the result should be



let data = {
weight: '',
DOB: '',
gender: ''
}


Give me some advice to achieve it.



Thanks in advance.










share|improve this question






















  • Oh, you can't merge that way. You gotta set those values to undefined, right?
    – Praveen Kumar Purushothaman
    yesterday










  • If your default object always has the 3 keys, why not just check each different object for those properties? If they exist, take them, if not, ignore.
    – tomerpacific
    yesterday










  • @tomerpacific I can check there's existing or not, but I need to preserve the default object form.
    – zynkn
    yesterday














0












0








0







I wanna merge 2 objects.



One object is the default form.



let data = {
weight: '',
DOB: '',
gender: '',
}


The other is irregular like below that



let temp1 = {
weight: '19.2',
}

let temp2 = {
DOB: '1992-11',
}

let temp3 = {
DOB: '1992-11',
gender: 'Male',
}

let temp4 = undefined;


If I merge data and temp1, the result should be



let data = {
weight: '19.2',
DOB: '',
gender: ''
}


If I merge data and temp3, the result should be



let data = {
weight: '',
DOB: '1992-11',
gender: 'Male',
}


If I merge data and temp4, the result should be



let data = {
weight: '',
DOB: '',
gender: ''
}


Give me some advice to achieve it.



Thanks in advance.










share|improve this question













I wanna merge 2 objects.



One object is the default form.



let data = {
weight: '',
DOB: '',
gender: '',
}


The other is irregular like below that



let temp1 = {
weight: '19.2',
}

let temp2 = {
DOB: '1992-11',
}

let temp3 = {
DOB: '1992-11',
gender: 'Male',
}

let temp4 = undefined;


If I merge data and temp1, the result should be



let data = {
weight: '19.2',
DOB: '',
gender: ''
}


If I merge data and temp3, the result should be



let data = {
weight: '',
DOB: '1992-11',
gender: 'Male',
}


If I merge data and temp4, the result should be



let data = {
weight: '',
DOB: '',
gender: ''
}


Give me some advice to achieve it.



Thanks in advance.







javascript object






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked yesterday









zynkn

3,90621030




3,90621030












  • Oh, you can't merge that way. You gotta set those values to undefined, right?
    – Praveen Kumar Purushothaman
    yesterday










  • If your default object always has the 3 keys, why not just check each different object for those properties? If they exist, take them, if not, ignore.
    – tomerpacific
    yesterday










  • @tomerpacific I can check there's existing or not, but I need to preserve the default object form.
    – zynkn
    yesterday


















  • Oh, you can't merge that way. You gotta set those values to undefined, right?
    – Praveen Kumar Purushothaman
    yesterday










  • If your default object always has the 3 keys, why not just check each different object for those properties? If they exist, take them, if not, ignore.
    – tomerpacific
    yesterday










  • @tomerpacific I can check there's existing or not, but I need to preserve the default object form.
    – zynkn
    yesterday
















Oh, you can't merge that way. You gotta set those values to undefined, right?
– Praveen Kumar Purushothaman
yesterday




Oh, you can't merge that way. You gotta set those values to undefined, right?
– Praveen Kumar Purushothaman
yesterday












If your default object always has the 3 keys, why not just check each different object for those properties? If they exist, take them, if not, ignore.
– tomerpacific
yesterday




If your default object always has the 3 keys, why not just check each different object for those properties? If they exist, take them, if not, ignore.
– tomerpacific
yesterday












@tomerpacific I can check there's existing or not, but I need to preserve the default object form.
– zynkn
yesterday




@tomerpacific I can check there's existing or not, but I need to preserve the default object form.
– zynkn
yesterday












2 Answers
2






active

oldest

votes


















5














You can use Object.assign():



let data = {
weight: '',
DOB: '',
gender: '',
}


let temp3 = {
DOB: '1992-11',
gender: 'Male',
}

data = Object.assign(data, temp3);


or if you don't want the data object to change:



let data2 = Object.assign({}, data, temp3);


The spread operator can also be used, which basically is the same, just that the original data object doesn't change:



data = { ...data, ...temp3 };





share|improve this answer























  • It's exactly what I found, I have known the spread operator but I didn't know exactly how to use. Thank you a lot
    – zynkn
    yesterday



















1














with jQuery you can use $.extend()






let data = {
weight: '',
DOB: '',
gender: '',
}
let temp1 = {
weight: '19.2',
}

console.log($.extend(data,temp1))

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>





OR use javascript spread operator ... as following






let data = {
weight: '',
DOB: '',
gender: '',
}

let temp3 = {
DOB: '1992-11',
gender: 'Male',
}

data={...data,...temp3}
console.log(data)





If the keys are same then the value from righmost object is used






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53944631%2fhow-to-merge-irregular-object%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    5














    You can use Object.assign():



    let data = {
    weight: '',
    DOB: '',
    gender: '',
    }


    let temp3 = {
    DOB: '1992-11',
    gender: 'Male',
    }

    data = Object.assign(data, temp3);


    or if you don't want the data object to change:



    let data2 = Object.assign({}, data, temp3);


    The spread operator can also be used, which basically is the same, just that the original data object doesn't change:



    data = { ...data, ...temp3 };





    share|improve this answer























    • It's exactly what I found, I have known the spread operator but I didn't know exactly how to use. Thank you a lot
      – zynkn
      yesterday
















    5














    You can use Object.assign():



    let data = {
    weight: '',
    DOB: '',
    gender: '',
    }


    let temp3 = {
    DOB: '1992-11',
    gender: 'Male',
    }

    data = Object.assign(data, temp3);


    or if you don't want the data object to change:



    let data2 = Object.assign({}, data, temp3);


    The spread operator can also be used, which basically is the same, just that the original data object doesn't change:



    data = { ...data, ...temp3 };





    share|improve this answer























    • It's exactly what I found, I have known the spread operator but I didn't know exactly how to use. Thank you a lot
      – zynkn
      yesterday














    5












    5








    5






    You can use Object.assign():



    let data = {
    weight: '',
    DOB: '',
    gender: '',
    }


    let temp3 = {
    DOB: '1992-11',
    gender: 'Male',
    }

    data = Object.assign(data, temp3);


    or if you don't want the data object to change:



    let data2 = Object.assign({}, data, temp3);


    The spread operator can also be used, which basically is the same, just that the original data object doesn't change:



    data = { ...data, ...temp3 };





    share|improve this answer














    You can use Object.assign():



    let data = {
    weight: '',
    DOB: '',
    gender: '',
    }


    let temp3 = {
    DOB: '1992-11',
    gender: 'Male',
    }

    data = Object.assign(data, temp3);


    or if you don't want the data object to change:



    let data2 = Object.assign({}, data, temp3);


    The spread operator can also be used, which basically is the same, just that the original data object doesn't change:



    data = { ...data, ...temp3 };






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited yesterday

























    answered yesterday









    PierreDuc

    28.9k45376




    28.9k45376












    • It's exactly what I found, I have known the spread operator but I didn't know exactly how to use. Thank you a lot
      – zynkn
      yesterday


















    • It's exactly what I found, I have known the spread operator but I didn't know exactly how to use. Thank you a lot
      – zynkn
      yesterday
















    It's exactly what I found, I have known the spread operator but I didn't know exactly how to use. Thank you a lot
    – zynkn
    yesterday




    It's exactly what I found, I have known the spread operator but I didn't know exactly how to use. Thank you a lot
    – zynkn
    yesterday













    1














    with jQuery you can use $.extend()






    let data = {
    weight: '',
    DOB: '',
    gender: '',
    }
    let temp1 = {
    weight: '19.2',
    }

    console.log($.extend(data,temp1))

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>





    OR use javascript spread operator ... as following






    let data = {
    weight: '',
    DOB: '',
    gender: '',
    }

    let temp3 = {
    DOB: '1992-11',
    gender: 'Male',
    }

    data={...data,...temp3}
    console.log(data)





    If the keys are same then the value from righmost object is used






    share|improve this answer




























      1














      with jQuery you can use $.extend()






      let data = {
      weight: '',
      DOB: '',
      gender: '',
      }
      let temp1 = {
      weight: '19.2',
      }

      console.log($.extend(data,temp1))

      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>





      OR use javascript spread operator ... as following






      let data = {
      weight: '',
      DOB: '',
      gender: '',
      }

      let temp3 = {
      DOB: '1992-11',
      gender: 'Male',
      }

      data={...data,...temp3}
      console.log(data)





      If the keys are same then the value from righmost object is used






      share|improve this answer


























        1












        1








        1






        with jQuery you can use $.extend()






        let data = {
        weight: '',
        DOB: '',
        gender: '',
        }
        let temp1 = {
        weight: '19.2',
        }

        console.log($.extend(data,temp1))

        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>





        OR use javascript spread operator ... as following






        let data = {
        weight: '',
        DOB: '',
        gender: '',
        }

        let temp3 = {
        DOB: '1992-11',
        gender: 'Male',
        }

        data={...data,...temp3}
        console.log(data)





        If the keys are same then the value from righmost object is used






        share|improve this answer














        with jQuery you can use $.extend()






        let data = {
        weight: '',
        DOB: '',
        gender: '',
        }
        let temp1 = {
        weight: '19.2',
        }

        console.log($.extend(data,temp1))

        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>





        OR use javascript spread operator ... as following






        let data = {
        weight: '',
        DOB: '',
        gender: '',
        }

        let temp3 = {
        DOB: '1992-11',
        gender: 'Male',
        }

        data={...data,...temp3}
        console.log(data)





        If the keys are same then the value from righmost object is used






        let data = {
        weight: '',
        DOB: '',
        gender: '',
        }
        let temp1 = {
        weight: '19.2',
        }

        console.log($.extend(data,temp1))

        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>





        let data = {
        weight: '',
        DOB: '',
        gender: '',
        }
        let temp1 = {
        weight: '19.2',
        }

        console.log($.extend(data,temp1))

        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>





        let data = {
        weight: '',
        DOB: '',
        gender: '',
        }

        let temp3 = {
        DOB: '1992-11',
        gender: 'Male',
        }

        data={...data,...temp3}
        console.log(data)





        let data = {
        weight: '',
        DOB: '',
        gender: '',
        }

        let temp3 = {
        DOB: '1992-11',
        gender: 'Male',
        }

        data={...data,...temp3}
        console.log(data)






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited yesterday

























        answered yesterday









        Elish

        1718




        1718






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53944631%2fhow-to-merge-irregular-object%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas