Comment puis-je fusionner les propriétés de deux objets JavaScript dynamiquement

Mots clés : javascriptjavascript-objectsjavascript

meilleur 5 Réponses Comment puis-je fusionner les propriétés de deux objets JavaScript dynamiquement

vote vote

97

let merged = {...obj1, ...obj2}; 
/** There's no limit to the number of objects you can merge.  *  Later properties overwrite earlier properties with the same name. */ const allRules = {...obj1, ...obj2, ...obj3}; 
/* For the case in question, you would do: */ Object.assign(obj1, obj2);  /** There's no limit to the number of objects you can merge.  *  All objects get merged into the first object.   *  Only the object in the first argument is mutated and returned.  *  Later properties overwrite earlier properties with the same name. */ const allRules = Object.assign({}, obj1, obj2, obj3, etc); 
for (var attrname in obj2) { obj1[attrname] = obj2[attrname]; } 
/**  * Overwrites obj1's values with obj2's and adds obj2's if non existent in obj1  * @param obj1  * @param obj2  * @returns obj3 a new object based on obj1 and obj2  */ function merge_options(obj1,obj2){     var obj3 = {};     for (var attrname in obj1) { obj3[attrname] = obj1[attrname]; }     for (var attrname in obj2) { obj3[attrname] = obj2[attrname]; }     return obj3; } 
vote vote

82

// Merge options object into settings object var settings = { validate: false, limit: 5, name: "foo" }; var options  = { validate: true, name: "bar" }; jQuery.extend(settings, options);  // Now the content of settings object is the following: // { validate: true, limit: 5, name: "bar" } 
var defaults = { validate: false, limit: 5, name: "foo" }; var options = { validate: true, name: "bar" };  /* Merge defaults and options, without modifying defaults */ var settings = $.extend({}, defaults, options);  // The content of settings variable is now the following: // {validate: true, limit: 5, name: "bar"} // The 'defaults' and 'options' variables remained the same. 
vote vote

74

Object.assign(obj1, obj2); 
vote vote

70

/* * Recursively merge properties of two objects  */ function MergeRecursive(obj1, obj2) {    for (var p in obj2) {     try {       // Property in destination object set; update its value.       if ( obj2[p].constructor==Object ) {         obj1[p] = MergeRecursive(obj1[p], obj2[p]);        } else {         obj1[p] = obj2[p];        }      } catch(e) {       // Property in destination object not set; create it and set its value.       obj1[p] = obj2[p];      }   }    return obj1; } 
o1 = {  a : 1,         b : 2,         c : {           ca : 1,           cb : 2,           cc : {             cca : 100,             ccb : 200 } } };  o2 = {  a : 10,         c : {           ca : 10,           cb : 20,            cc : {             cca : 101,             ccb : 202 } } };  o3 = MergeRecursive(o1, o2); 
o3 = {  a : 10,         b : 2,         c : {           ca : 10,           cb : 20,           cc : {              cca : 101,             ccb : 202 } } }; 
vote vote

57

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

Questions similaires