Answers for "search for diff in two JSON"

0

search for diff in two JSON

function getDifference(o1, o2) {
    var diff = {};
    var tmp = null;
    if (JSON.stringify(o1) === JSON.stringify(o2)) return;

    for (var k in o1) {
      if (Array.isArray(o1[k]) && Array.isArray(o2[k])) {
        tmp = o1[k].reduce(function (p, c, i) {
          var _t = getDifference(c, o2[k][i]);
          if (_t)
            p.push(_t);
          return p;
        }, []);
        if (Object.keys(tmp).length > 0)
          diff[k] = tmp;
      } else if (typeof (o1[k]) === "object" && typeof (o2[k]) === "object") {
        tmp = getDifference(o1[k], o2[k]);
        if (tmp && Object.keys(tmp) > 0)
          diff[k] = tmp;
      } else if (o1[k] !== o2[k]) {
        diff[k] = o2[k]
      }
    }
    return diff;
  }
Posted by: Guest on March-07-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language