I am trying to group campaigns in my data by the sales person that created created them but keep going around in circles with different JS array methods.
I have filtered my main data set to return those that have the user role of sales person.
I have my campaign data that shows who created the campaigns
I now have to to group them by sales person.
In my mind I saw it as loop/map through each campaign object in campaigns array, check the createdBy name and return a new array of objects that have the results group by createdBy name.
Sounded easy in my head but can't get around it.
I have put together the following:
const filtered = usersData.filter((val) => val.role === 'sales');
// this returns only the sales people out of all the possible user roles in the following format:
active: true
createdAt: "2019-04-11T21:00:00.000Z"
email: "[email protected]"
name: "Alexander Tom Jones"
phone: 3044283743
role: "sales"
_id: "5c8a20d32f8fb814b56fa187"
// my campaign data contains the name of the sales person:
budget: 57154564
company: "sales company"
createdAt: "2020-07-30T09:03:51.925Z"
createdBy: "sales user"
creator_id: "5f228c8d58769fc7d556a6fa"
endDate: "2020-11-08T00:00:00.000Z"
isDeleted: false
location: "Helsinki"
name: "sales test campaign"
// with this function I can return all of the campaigns created by a sales person 1 by 1:
const filteredCampaigns = campaignData.filter(
(val) => val.createdBy === 'sales user'
);
I just cant figure out how to "loop" through each sales person.
I have looked at map / reduce and groupby solutions but nothing comes remotely close. Where am I going wrong?