Home Java Object Merging(v 1.1)

Object Merging(v 1.1)

by admin

Hello Habr!
Just yesterday the need to merge two javascript objects came up. After digging around on the internet, I found out that the only way to merge two objects is to copy fields from one to the other. ("Tearing, " thought Stirlitz.) With a bit of forethought he created his bicycle method for this case.
Basic Capabilities :

  1. copying properties of one object to another with recursive traversal of the latter, the output is an object that contains all the properties of the first and second object.
  2. property copying without recursive traversal of the source object, in which 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 there are two options :
  1. with both properties preserved – an array of values is formed
  2. replacing the original with the target

  • Copy depth limitation
  • And the function itself :

    /***Function to copy properties of one object to another* @param objS <i> object</i> object from which properties will be copied* @param objT <i> object</i> object to which properties will be copied* @param rec<i> bool</i> flag to recursively bypass the source object to completely* all properties will otherwise be copied "as"* @param repl<i> bool</i> flag to replace property values with the same name* if it is not defined, it will create an array of values* @param max_l_rec <i> int</i> maximum recursion depth (change with care)* at value 1, objects are copied "as"*/function objUnion(objS, objT, rec, repl, max_l_rec){//count the number of elements in the source objectvar count_elem = objS.length;//For recursion control we create two auxiliary fields in the target object, The //first is the depth of recursion, the second is the number of elements counted, //to know when the end of copying is reached and the specified fields can be deletedif(!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 the maximum level of recursion is reachedif(max_l_rec objT.cur_l_rec == max_l_rec){objUnion(objS, objT, rec, repl, max_l_rec);delete objT.cur_l_rec ;return;}//start copyingfor(var key in objS){//if the current field is an object//and a recursive traversal parameter is defined - let's look inside itif(typeof(objS[key]) == 'object'){if(rec ((max_l_rec objT.cur_rec <max_l_rec)|| !max_l_rec)){objUnion(objS[key], objT, rec, repl, max_l_rec);objT.cur_l_rec = cur_l_rec;//if we return from the depths, we add one to the copied elementsif(objT.cur_l_rec == 0){objT.summ_elem ++;}continue;}}//determine what to do with the fields which have the same name//also create from them an array or change the value of the first field into the second oneif(objT[key] !repl){objT[key] = [objS[key], objT[key]];}else{objT[key] = objS[key];}}//if all objects have their properties copied, remove the service fieldsif((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 ;}}//small testvar 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'};//link to the object to be copied tovar link = obj2objUnion(obj1, link, 1, 0, 1);//remove the linklink = 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 with jQuery and the extend function, but don’t plug in the whole framework for the sake of using one function.
    P.S. huge request, if you don’t like my feature write in the comments what’s wrong with it, I’ll be happy to correct it to the good.

    You may also like