object-array-diff
v0.0.1
Published
Get the diff from object A to object B, including object arrays
Downloads
5
Readme
object-array-diff
Get the diff of objectA and objectB. If your object is an array of objects or contains an array of objects only the changed objects in the array are returned. (See examples 2 and 3)
Usage
Example 1 - Object
var diff = require('object-array-diff');
var original = {
bacon: true,
howMuch: 54,
flavor: 'tastey'
};
var changed = {
bacon: false, //changed
howMuch: 10, //changed
flavor: 'tastey',
ham: true //added
};
diff(original, changed);
/*
{
bacon: false,
howMuch: 10,
ham: true
}
*/
Example 2 - Array of Objects
var diff = require('object-array-diff');
var original = [
{
id: 0,
name: 'cyrus'
},
{
id: 1,
name: 'andrew'
}
];
var changed = [
{
id: 0,
name: 'cyrus'
},
{
id: 1,
name: 'randy' //changed key
},
{ //added object
name: 'alex'
}
];
diff(original, changed);
/*
{
"name": "randy"
},
{
"name": "alex"
}
*/
Configure object-array-diff to always return id
in order to tell what items changed in the ordered array without returning full array.
diff({alwaysReturn: ['id']});
diff(original, changed);
/*
{
id: 1,
name: "randy"
},
{
name: "alex"
}
*/
Example 3 - Objects containing arrays of objects
var diff = require('object-array-diff');
diff({alwaysReturn: ['id']});
var original = {
name: 'my object',
description: 'oh me oh my',
details: {
contacts: [
{
id: 0,
name: 'cyrus',
email: '[email protected]',
deleted: null
},
{
id: 1,
name: 'alex',
email: '[email protected]'
}
]
}
};
var changed = {
name: 'New name', //changed key
description: 'oh me oh my',
newKey: 'added a key', //added key
details: {
contacts: [
{
id: 0,
name: 'cyrus',
email: '[email protected]',
deleted: 0 //changed key
},
{
id: 1,
name: 'alex',
email: '[email protected]'
},
{ //added object
name: 'andrew',
email: '[email protected]'
}
]
}
};
diff(original, changed);
/*
{
name: "New name",
newKey: "added a key",
details: {
contacts: [
{
"id": 0,
"deleted": 0
},
{
"name": "andrew",
"email": "[email protected]"
}
]
}
}
*/