Javascript: Finding differences

JS: Objects
Last update: 14 Sep 00:11
0
Students

Sometimes when coding, you have to find the difference between two data sets, such as objects. For example, when searching for differences in JSON files. There are even special services for this, such as http://www.jsondiff.com/ (try clicking on the sample data link and then the Compare button).

solution.js

Implement and export as default a function that compares two objects and returns the result of the comparison as an object. The keys of the resulting object will be all the keys from the two input objects, and the value will be a string describing the differences: added, deleted, changed, or unchanged.

Possible values are:

  • added — the key was missing in the first object, but was added to the second one
  • deleted — the key was in the first object, but is missing in the second one
  • changed — the key was in both the first and the second objects, but the values are different
  • unchanged — the key was was in the first and second objects with the same values
import genDiff from './diffGenerator.js';

genDiff(
  { one: 'eon', two: 'two', four: true },
  { two: 'own', zero: 4, four: true },
);
// {
//   one: 'deleted',
//   two: 'changed',
//   four: 'unchanged',
//   zero: 'added',
// }

For full access to the challenge you need a professional subscription.

A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.

Get access
130
courses
1000
exercises
2000+
hours of theory
3200
tests