📜 ⬆️ ⬇️

Combining objects (v 1.1)

Hi Habr!
Just yesterday, it became necessary to combine two javascript objects. Having rummaged through the Internet, I learned that the only way to combine two objects is to copy fields from one to another. ("Discontinuous" - thought Stirlitz) Having pondered, he created his own bicycle method for this business.

Key features:
  1. copying the properties of one object to another with a recursive traversal of the latter, the output is an object containing all the properties of the first and second.
  2. copying properties without recursive traversal of the source object, in this case, the output will be an object that preserves the original structure of the first and second objects
  3. in case two objects have properties with the same name, two scenarios are provided for:
    1. with both properties preserved - an array of values ​​is formed
    2. with the replacement of the original target

  4. Copy depth limit

Well, actually the function itself:
/** *       * @param objS <i>object</i>       * @param objT <i>&object</i>       * @param rec <i>bool</i>    -   *        " " * @param repl <i>bool</i>        *     -     * @param max_l_rec <i>int</i>   (  ) *   1   " " */ function objUnion(objS, objT, rec, repl, max_l_rec) { //      var count_elem = objS.length; //         , // -  ,  -   , // ,          if(!objT.hasOwnProperty("cur_l_rec")) { objT.cur_l_rec = 0; objT.summ_elem = 0; var cur_l_rec = 0; } else { objT.cur_l_rec++; cur_l_rec = objT.cur_l_rec; } //     if(max_l_rec && objT.cur_l_rec == max_l_rec) { objUnion(objS, objT, rec, repl, max_l_rec); delete objT.cur_l_rec ; return; } //  for(var key in objS) { //     //     -    if(typeof(objS[key]) == 'object') { if(rec && ((max_l_rec && objT.cur_l_rec < max_l_rec)|| !max_l_rec)) { objUnion(objS[key], objT, rec, repl, max_l_rec); objT.cur_l_rec = cur_l_rec; //          if(objT.cur_l_rec == 0) { objT.summ_elem ++; } continue; } } //,    ,    //    ,       if(objT[key] && !repl) { objT[key] = [objS[key],objT[key]]; } else { objT[key] = objS[key]; } } //         if((objT.summ_elem == count_elem || !objS.hasOwnProperty('length')) && (objT.cur_l_rec == 0 && cur_l_rec == 0)) { delete objT.cur_l_rec ; delete objT.summ_elem ; } } //  var obj1 = [ { field4 : { field9 : { field10 : 1 }, field8 : 2 }, field5 : 3 }, { field6 : 5, field7 : { field11 : 'green', field12 : 'black' } }, { field13 : 5, field14 : { field15 : 'green', field16 : 'black' } } ]; var obj2 = { field1 : 'green', field2 : 'square', field3 : 'small' }; //       var link = obj2 objUnion(obj1, link, 1, 0, 1); //  link = null; console.log(obj2); 


Thank you all for your attention, I hope my bike method will be useful to someone.

There is a solution to this problem using jQuery and the extend function, but not to include the whole framework for the sake of using one function.
')
PS a huge request, if you do not like my function, write in the comments SPECIFICALLY that it is bad, I will correct it with pleasure, for good.

Source: https://habr.com/ru/post/139075/


All Articles