Classes
Methods
# (static) Diff(first, second) → {array}
With the diff function you can perform the change of one object to another. The result shows the changes of the second object to the first object.
The operator add
means that something has been added to the second object. delete
means that something has been deleted from the second object compared to the first object.
You can call the method via the monster namespace Monster.Data.Diff()
.
<script type="module">
import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.8.0/dist/modules/data/diff.js';
console.log(Monster.Data.Diff(a, b))
</script>
Alternatively, you can also integrate this function individually.
<script type="module">
import {Diff} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.8.0/dist/modules/data/diff.js';
console.log(Diff(a, b))
</script>
Parameters:
Name | Type | Description |
---|---|---|
first |
* | |
second |
* |
- Since:
- 1.6.0
- Copyright:
- schukai GmbH
Returns:
- Type
- array
Example
import {Diff} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.8.0/dist/modules/data/diff.js';
// given are two objects x and y.
let x = {
a: 1,
b: "Hello!"
}
let y = {
a: 2,
c: true
}
// These two objects can be compared with each other.
console.log(Diff(x, y));
// the result is then the following
//
// [
// {
// operator: 'update',
// path: [ 'a' ],
// first: { value: 1, type: 'number' },
// second: { value: 2, type: 'number' }
// },
// {
// operator: 'delete',
// path: [ 'b' ],
// first: { value: 'Hello!', type: 'string' }
// },
// {
// operator: 'add',
// path: [ 'c' ],
// second: { value: true, type: 'boolean' }
// }
// ]