I appreciate any insight on this. My understanding of .filter is that it creates a brand new array of the with the filtered data if it finds any. I have a piece of code:
const pieChartData = data.reduce((newList, item) => {
const {chg_organization_zChgManager_combo_name} = item;
const hasName = newList.filter(item => item.name === chg_organization_zChgManager_combo_name);
if (hasName.length > 0) {
hasName[0].value++;
return [...newList, ...hasName];
} else {
return [...newList, {'name': chg_organization_zChgManager_combo_name, 'value': 1}]
}
}, []);
This breaks and goes into a massive endless loop. Yet if I run this slightly modified version it works exactly as intended:
const pieChartData = data.reduce((newList, item) => {
const {chg_organization_zChgManager_combo_name} = item;
const hasName = newList.filter(item => item.name === chg_organization_zChgManager_combo_name);
if (hasName.length > 0) {
hasName[0].value++;
return newList;
} else {
return [...newList, {'name': chg_organization_zChgManager_combo_name, 'value': 1}]
}
}, []);
My initial guess is that instead of creating a brand new array it is actually creating an instance and when modifying the instance of the array it modifies the array that instance was created from (thus modifying it mutably rather than immutably like I was attempting to do).
This reduce function is taking an array of objects and building a new array of objects that will eventually create a value of every occurrance the chg_organization_zChgManager_combo_name shows up so we can built a chart on how many items are assigned to said manager.
I appreciate any feedback as I really expected that first version of the code to work as intended.