I am in need of merging multiple JavaScript objects recursively. These objects contain number properties that need to be added together during the merge process. Additionally, they also have Map properties. Below is an example of how my object appears in the Developer Console:
byGender:Map 0:{"Male" => 9} key:"Male" value:9 1:{"Female" => 11} key:"Female" value:11 byType:Map 0:{"Teens" => Object} key:"Teens" value:Object byGender:Map 0:{"Guys" => 7} key:"Guys" value:7 1:{"Girls" => 10} key:"Girls" value:10 total:17 1:{"Chaperones" => Object} key:"Chaperones" value:Object byGender:Map 0:{"Ladies" => 1} key:"Ladies" value:1 1:{"Men" => 2} key:"Men" value:2 total:3 total:42
My goal is to merge these Map objects and add the numbers of matching keys together.
I'm currently using _.mergeWith along with a customizer function. While I am able to merge simple number properties successfully, I am facing difficulty in merging the Map objects as well. Currently, my customizer merger function replaces the Map object, causing the last one to overwrite the previous. Here's the customizer function I am using with _.mergeWith:
var merger = function (a, b) { if (_.isNumber(a)) { return a + b; } else { return _.mergeWith(a, b, merger); } };
Searching for solutions has been challenging because the keyword "map" mainly returns results related to .map(), rather than focusing on the Map object itself.