3

I am comparing two javascript objects. How can I list all the differences using optimized code?

I tried few plugins deep-diff & lodash as well.Lodash is returning me difference of the same line but not any other added field.

const a = {
"id":1,
"name":xyz
}

const b = {
"id":1,
"name":xyz abc,
"address":pqr
}

I expect the output: {"name":xyz abc","address":pqr}

2 Answers 2

2
    Object.keys(a).forEach(x => { if (Object.keys(b).every(y => b[y] !== a[x])) { diff[x] = a[x] } })
    Object.keys(b).forEach(x => { if (Object.keys(a).every(y => a[y] !== b[x])) { diff[x] = b[x] } })
    console.log(diff)
Sign up to request clarification or add additional context in comments.

Comments

0

Here is an example of what you want using lodash functions.

https://gist.github.com/Yimiprod/7ee176597fef230d1451

1 Comment

Hey... I tried this earlier but it wasn't returning newly added arrays or any other added field...It was returning difference only on the same line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.