What’s the best/standard way of merging two associative arrays in JavaScript? Does everyone just do it by rolling their own for loop?
What’s the best/standard way of merging two associative arrays in JavaScript? Does everyone just
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
with jquery you can call
$.extend(assoc. arrays are objects in js)
look here: http://api.jquery.com/jQuery.extend/
edit: Like rymo suggested, it’s better to do it this way:
As here obj1 (and obj2) remain unchanged.
edit2: In 2018 the way to do it is via
Object.assign:If working with ES6 this can be achieved with the Spread Operator: