Can someone show me how to return the new data when comparing something like this. using vanilla JavaScript.
{
"48": "{\"sid\":\"48\",\"name\":\"title 1\"}",
"77": "{\"sid\":\"77\",\"name\":\"The blahblah title\"}"
}
compared to this
{
"48": "{\"sid\":\"48\",\"name\":\"title 1\"}",
"77": "{\"sid\":\"77\",\"name\":\"The blahblah title\"}",
"83": "{\"sid\":\"83\",\"name\":\"The blahblah derp\"}",
"87": "{\"sid\":\"87\",\"name\":\"The derpy title 4\"}"
}
it should return only the differences.
{
"83": "{\"sid\":\"83\",\"name\":\"The blahblah derp\"}",
"87": "{\"sid\":\"87\",\"name\":\"The derpy title 4\"}"
}
You can use Object.keys() and Array.includes() to do that.
var data = {
"48": "{\"sid\":\"48\",\"name\":\"title 1\"}",
"77": "{\"sid\":\"77\",\"name\":\"The blahblah title\"}",
"83": "{\"sid\":\"83\",\"name\":\"The blahblah derp\"}",
"87": "{\"sid\":\"87\",\"name\":\"The derpy title 4\"}"
};
var obj1 = {
"48": "{\"sid\":\"48\",\"name\":\"title 1\"}",
"77": "{\"sid\":\"77\",\"name\":\"The blahblah title\"}"
};
var result = {};
var keys = Object.keys(obj1);
for (var key in data) {
if (!keys.includes(key)) {
result[key] = data[key];
}
}
console.log(result);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With