|
I have a javascript function which merges two objects recursively. I call it in a reduce() to merge documents and add their numbers. This function works fine on node.js, and in the Chrome console. (so it's not a v8 problem)
merge_and_sum_recursive_in_place = function(obj1, obj2) {
|
var k, v;
|
if (obj1 == null) {
|
return obj2;
|
}
|
if (obj2 == null) {
|
return obj1;
|
}
|
for (k in obj2) {
|
v = obj2[k];
|
if (v.constructor === Object) // THIS NEVER RETURNS TRUE
|
// if (typeof(v) === 'object') { // THIS WORKS
|
obj1[k] = merge_and_sum_recursive_in_place(obj1[k], v);
|
} else {
|
if (obj1[k] != null) {
|
if (v.constructor === Number) { // THIS WORKS
|
obj1[k] += v;
|
} else {
|
obj1[k] = v;
|
}
|
} else {
|
obj1[k] = v;
|
}
|
}
|
}
|
return obj1;
|
};
|
All the numbers in the subdocuments are never added if I use "v.constructor === Object", but it works fine if I use "typeof(v) === 'object'".
Note that "v.constructor === Number" works perfectly.
Perhaps subdocument objects parsed from BSON are not "real" javascript "Object"s ?
|