Skip to content

Deleted result is missing fields that were removed in target. #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
JohnGrantPGA opened this issue Jun 13, 2017 · 2 comments
Closed

Deleted result is missing fields that were removed in target. #10

JohnGrantPGA opened this issue Jun 13, 2017 · 2 comments

Comments

@JohnGrantPGA
Copy link

These are my source and target objects and my result of a detailed diff. Shouldn't the deleted property contain, field3?

$ node app.js
{
    "a": [
        {
            "field1": "value1",
            "field2": "value2",
            "field3": "value3"
        }
    ],
    "b": [
        {
            "field1": "value1",
            "field2": "value2"
        }
    ],
    "result": {
        "added": {},
        "deleted": {
            "0": {}
        },
        "updated": {}
    }
}
@mattphillips
Copy link
Owner

Hey @JohnGrantPGA I suspect that the issue is because the difference between a and b is being converted into JSON with JSON.stringify.

undefined is not supported in JSON, so any key/ value pairing that is undefined will be filtered out when using JSON.stringify.

What you could do instead is supply a function to JSON.stringify(obj, fn) to convert any undefined values into something of your choosing 😄.

Heres an example that converts undefined values to null:

function replacer(key, value) {
  if (value == undefined) {
    return null;
  }
  return value;
}

const a = [{
  "field1": "value1",
  "field2": "value2",
  "field3": "value3"
}];
const b = [{
  "field1": "value1",
  "field2": "value2"
}];

const difference = detailedDiff(a, b);

console.log(JSON.stringify(difference, replacer));
// {"added":{},"deleted":{"0":{"field3":null}},"updated":{}}

@JohnGrantPGA
Copy link
Author

Hi Matt,

Thanks for the quick response! You're spot on. I incorporated your suggestions. Thank you. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants