Skip to content

Instantly share code, notes, and snippets.

@blairg
Created September 20, 2017 21:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blairg/26becdab7c30e0dfd32c719ffd29c3b0 to your computer and use it in GitHub Desktop.
Save blairg/26becdab7c30e0dfd32c719ffd29c3b0 to your computer and use it in GitHub Desktop.
Example of how to merge the values of 2 objects in JavaScript
'use strict';
var _deepDiff = require('deep-diff');
var _objectAssignDeep = require('object-assign-deep');
var _objectAssignDeep2 = _interopRequireDefault(_objectAssignDeep);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var anyChanges = function anyChanges(newObject, originalObject) {
return (0, _deepDiff.diff)(newObject, originalObject).length > 0;
};
var mergeChanges = function mergeChanges(newObject, originalObject) {
return !anyChanges(newObject, originalObject) ? originalObject : (0, _objectAssignDeep2.default)(originalObject, newObject);
};
var london1801 = {
innerLondon: 879491,
outerLondon: 131666,
greaterLondon: 1011157,
country: 'England'
};
var london2001 = {
innerLondon: 2765975,
outerLondon: 4406061,
greaterLondon: 7172036,
country: 'England'
};
console.dir(mergeChanges(london2001, london1801));
import { diff } from 'deep-diff';
import objectAssignDeep from 'object-assign-deep';
const anyChanges = (newObject, originalObject) => {
return diff(newObject, originalObject).length > 0;
};
const mergeChanges = (newObject, originalObject) => {
return !anyChanges(newObject, originalObject) ? originalObject : objectAssignDeep(originalObject, newObject);
};
const london1801 = {
innerLondon: 879491,
outerLondon: 131666,
greaterLondon: 1011157,
country: 'England',
};
const london2001 = {
innerLondon: 2765975,
outerLondon: 4406061,
greaterLondon: 7172036,
country: 'England',
};
console.dir(mergeChanges(london2001, london1801));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment