I’m trying to figure out the best (simplest?) way to create maps for all of the properties of an object of objects. For example:
const customerData = {
customer1: {
region: "south",
market: "education",
salesRep: "Bob"
},
customer2: {
region: "north",
market: "commerical",
salesRep: "Nancy"
}, ...
}
In my real world example, each customer has about 10 properties, each with a single primitive data type. I want to take and convert that to objects/maps that I can use to look up all the customers that have, say region == "north"
.
const regions = {
north: ["customer2", ...],
south: ["customer1", ...]
}
// OR
const regions = {
north: [customerData["customer2"], ...],
south: [customerData["customer1"], ...]
}
And doing that for all of the properties.
(If I weren’t storing these in Redux and using the Redux toolkit, I would probably do the latter, but it prefers not deeply nesting the data, so will probably go with the former.)
I can easily write the forEach/for of loops to create these objects.
But my real question is: Is there some higher level construct for this?
This seems like this would be a not uncommon thing, but I can’t think of a way of doing it without just spinning through the list of all customers and manually creating the Objects/Maps for each property.
I guess I could probably convert the customerData to an array and use .filter(), but this seems heavy (there are over 2000 customers).