Merging objects (associative arrays)

前端 未结 16 1215
野的像风
野的像风 2020-12-04 08:20

What’s the best/standard way of merging two associative arrays in JavaScript? Does everyone just do it by rolling their own for loop?

16条回答
  •  情深已故
    2020-12-04 09:00

    Underscore also has an extend method:

    Copy all of the properties in the source objects over to the destination object. It's in-order, so the last source will override properties of the same name in previous arguments.

    _.extend(destination, *sources) 
    
    _.extend({name : 'moe'}, {age : 50});
    => {name : 'moe', age : 50}
    

提交回复
热议问题