I have two javascript objects:
var a = {
x: 1,
y: {
faz: 'hello',
baz: ''
},
z: [1, 2]
};
var defaults = {
x: 2,
y: {
faz: '',
baz: ''
},
z: [1, 2]
};
I want to only keep the fields of a
that are different from the default:
a = remove_defaults(a, defaults); // <---- i need this fnc
{
x: 1,
y: {
faz: 'hello'
}
}
The goal is to remove default values from an object that serves as a state (via URL). The state can have nested fields, so a shallow compare is not enough. The leaf values are all primitive (number, string, bool).
(this is a bit like the opposite of underscore.js
's _.defaults()
method)
What is the best way to achieve this?
The solution can use underscore.js
if that helps, but no jquery
.
Try this:
function removeDuplicates(a, defaults, result) {
for (var i in a) {
if (i in defaults) {
if (typeof a[i] == "object"
&& typeof defaults[i] == "object"
&& Object.prototype.toString.call(defaults[i]) !== '[object Array]') {
result[i] = removeDuplicates(a[i], defaults[i], {});
} else if (a[i] !== defaults[i]) {
result[i] = a[i];
}
} else {
result[i] = a[i];
}
}
return result;
}
var result = removeDuplicates(a, defaults, {});
function remove_defaults(obj, defaults) {
for (var i in obj) {
if (typeof obj[i] == 'object') {
obj[i] = remove_defaults(obj[i], defaults[i]);
continue;
}
if (defaults[i] !== undefined && obj[i] == defaults[i]) {
delete obj[i];
}
}
return obj;
}
Fiddle: http://jsfiddle.net/ybVGq/
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