Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. exports.handler = (event, context, callback) => {
  2. var output = {};
  3. event.forEach(function(obj) { output = mergeJSON(output, obj); });
  4. context.succeed(output);
  5. };
  6.  
  7. var jsonC = {}.constructor;
  8.  
  9. var isJSON = function(json) {
  10. if (json && json.constructor === jsonC) {
  11. return true;
  12. } else {
  13. return false;
  14. }
  15. }
  16.  
  17. var mergeJSON = function(json1, json2) {
  18. var result = null;
  19. if (isJSON(json2)) {
  20. result = {};
  21. if (isJSON(json1)) {
  22. for (var key in json1) {
  23. result[key] = json1[key];
  24. }
  25. }
  26.  
  27. for (var key in json2) {
  28. if (typeof result[key] === “object” && typeof json2 === “object”) {
  29. result[key] = mergeJSON(result[key], json2[key]);
  30. } else {
  31. result[key] = json2[key];
  32. }
  33. }
  34. } else if (Array.isArray(json1) && Array.isArray(json2)) {
  35. result = json1;
  36.  
  37. for (var i = 0; i < json2.length; i++) {
  38. if (result.indexOf(json2[i]) === -1) {
  39. result[result.length] = json2[i];
  40. }
  41. }
  42. } else {
  43. result = json2;
  44. }
  45.  
  46. return result;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement