Guest User

Untitled

a guest
Jan 13th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. How to Optimize Merge of Two Objects That Include Arrays of Objects
  2. var utils = require("util");
  3.  
  4. function mergeObjs(def, obj) {
  5. if (typeof obj == 'undefined') {
  6. return def;
  7. } else if (typeof def == 'undefined') {
  8. return obj;
  9. }
  10.  
  11. for (var i in obj) {
  12. // if its an object
  13. if (obj[i] != null && obj[i].constructor == Object)
  14. {
  15. def[i] = mergeObjs(def[i], obj[i]);
  16. }
  17. // if its an array, simple values need to be joined. Object values need to be remerged.
  18. else if(obj[i] != null && utils.isArray(obj[i]) && obj[i].length > 0)
  19. {
  20. // test to see if the first element is an object or not so we know the type of array we're dealing with.
  21. if(obj[i][0].constructor == Object)
  22. {
  23. var newobjs = [];
  24. // create an index of all the existing object IDs for quick access. There is no way to know how many items will be in the arrays.
  25. var objids = {}
  26. for(var x= 0, l= def[i].length ; x < l; x++ )
  27. {
  28. objids[def[i][x].id] = x;
  29. }
  30.  
  31. // now walk through the objects in the new array
  32. // if the ID exists, then merge the objects.
  33. // if the ID does not exist, push to the end of the def array
  34. for(var x= 0, l= obj[i].length; x < l; x++)
  35. {
  36. var newobj = obj[i][x];
  37. if(objids[newobj.id] !== undefined)
  38. {
  39. def[i][x] = mergeObjs(def[i][x],newobj);
  40. }
  41. else {
  42. newobjs.push(newobj);
  43. }
  44. }
  45.  
  46. for(var x= 0, l = newobjs.length; x<l; x++) {
  47. def[i].push(newobjs[x]);
  48. }
  49. }
  50. else {
  51. for(var x=0; x < obj[i].length; x++)
  52. {
  53. var idxObj = obj[i][x];
  54. if(def[i].indexOf(idxObj) === -1) {
  55. def[i].push(idxObj);
  56. }
  57. }
  58. }
  59. }
  60. else
  61. {
  62. def[i] = obj[i];
  63. }
  64. }
  65. return def;}
  66.  
  67. var obj1 = {
  68. "name" : "myname",
  69. "status" : 0,
  70. "profile": { "sex":"m", "isactive" : true},
  71. "strarr":["one", "three"],
  72. "objarray": [
  73. {
  74. "id": 1,
  75. "email": "a1@me.com",
  76. "isactive":true
  77. },
  78. {
  79. "id": 2,
  80. "email": "a2@me.com",
  81. "isactive":false
  82. }
  83. ]
  84. };
  85. var obj2 = {
  86. "name" : "myname",
  87. "status" : 1,
  88. "newfield": 1,
  89. "profile": { "isactive" : false, "city": "new York"},
  90. "strarr":["two"],
  91. "objarray": [
  92. {
  93. "id": 1,
  94. "isactive":false
  95. },
  96. {
  97. "id": 2,
  98. "email": "a2modified@me.com"
  99. },
  100. {
  101. "id": 3,
  102. "email": "a3new@me.com",
  103. "isactive" : true
  104. }
  105. ]
  106. };
  107.  
  108. { name: 'myname',
  109. status: 1,
  110. profile: { sex: 'm', isactive: false, city: 'new York' },
  111. strarr: [ 'one', 'three', 'two' ],
  112. objarray:
  113. [ { id: 1, email: 'a1@me.com', isactive: false },
  114. { id: 2, email: 'a2modified@me.com', isactive: false },
  115. { id: 3, email: 'a3new@me.com', isactive: true } ],
  116. newfield: 1 }
Add Comment
Please, Sign In to add comment