I am having difficulty with this function to run in my React/Redux code. I am fairly new to React (mostly work on backend) so I am not sure why it's not running as expected. Is it in the right place? I am also having trouble finding where console.log is printing in the console, since the console is set up with prev action, action, next state patterns....
I have this function defined in my 'actions.js' (where it is also called later on):
const groupBy = (array, key) => {
return array.reduce((result, currentValue) => {(
result[currentValue[key]] = result[currentValue[key]] || []).push(
currentValue
);
return result;
}, {});
};
Here is where the function is called (same file):
export function getAlerts() {
return dispatch => {
api.get('notifications', (response) => {
const grouped = groupBy(response.results.data, 'email');
dispatch(updateFetchedAlerts(grouped));
}, (error) => {
console.warn(error);
})
}
}
The input, response.results.data, looks something like this:
[{"email": test@email.com, "is_active": true, "alert_id": 1, "pk": 1},
{"email": test@email.com, "is_active": true, "alert_id": 2, "pk": 2},
{"email": different@email.com, "is_active": true, "alert_id": 1, "pk": 3}]
I want it to look like this:
[{"test@email.com": [{"is_active": true, "alert_id": 1, "pk": 1},
{"is_active": true, "alert_id": 2, "pk": 2}],
"different@email.com": [{"is_active": true, "alert_id": 1, "pk": 3}]}]
but it seems to not be running this function, I've rerun yarn build and used incognito...
UPDATE: This function actually WORKS! Thanks all. The redux developer tools are very helpful. Now the second problem is I need to add in my own keys.... So ideally the result would look like this. Preferably no lodash!:
[{"email": "test@email.com",
"alerts": [{"is_active": true, "alert_id": 1, "pk": 1},
{"is_active": true, "alert_id": 2, "pk": 2}]},
{"email": "different@email.com",
"alerts": [{"is_active": true, "alert_id": 1, "pk": 3}]}]