search for diff in two json

Solutions on MaxInterview for search for diff in two json by the best coders in the world

showing results for - "search for diff in two json"
Frieda
05 Aug 2020
1function getDifference(o1, o2) {
2    var diff = {};
3    var tmp = null;
4    if (JSON.stringify(o1) === JSON.stringify(o2)) return;
5
6    for (var k in o1) {
7      if (Array.isArray(o1[k]) && Array.isArray(o2[k])) {
8        tmp = o1[k].reduce(function (p, c, i) {
9          var _t = getDifference(c, o2[k][i]);
10          if (_t)
11            p.push(_t);
12          return p;
13        }, []);
14        if (Object.keys(tmp).length > 0)
15          diff[k] = tmp;
16      } else if (typeof (o1[k]) === "object" && typeof (o2[k]) === "object") {
17        tmp = getDifference(o1[k], o2[k]);
18        if (tmp && Object.keys(tmp) > 0)
19          diff[k] = tmp;
20      } else if (o1[k] !== o2[k]) {
21        diff[k] = o2[k]
22      }
23    }
24    return diff;
25  }