I am building a keyword generator for an app, where it takes in a list of cities and generates those cities meshed into keyword groups by category.
I have tried doing a few other options with .map on the state and .find to get the matches, but I am struggling with properly merging things to look like my final solution. Any references or help to getting it done would be greatly appreciated.
const generatedKeywords = (state = [], action) => {
switch (action.type) {
case types.KEYWORD_GENERATOR_TOOL_ADD_GENERATED_KEYWORDS: {
return [...state, ...action.payload];
}
default: {
return state;
}
}
};
I currently pass in with an action something like:
[
{
cityName: "Los Angeles",
keywords: ["Los Angeles HVAC Repair", "HVAC Installation Los Angeles"]
},
{
cityName: "Sacramento",
keywords: ["Sacramento HVAC Repair", "HVAC Installation Sacramento"]
}
]
This does the job when I pass in the first action, but subsequent actions end us not merging the objects with the same cityName. This is
[
{
"cityName": "Los Angeles",
"keywords": [
[
"Los Angeles Roofing",
"Roofers in Los Angeles"
]
]
},
{
"cityName": "Sacramento",
"keywords": [
[
"Sacramento Roofing",
"Roofers in Sacramento"
]
]
},
{
"cityName": "Los Angeles",
"keywords": [
[
"HVAC Company in Los Angeles",
"Los Angeles HVAC Inspection"
]
]
},
{
"cityName": "Sacramento",
"keywords": [
[
"HVAC Company in Sacramento",
"Sacramento HVAC Inspection"
]
]
}
]
I would like it to look like this:
[
{
"cityName": "Los Angeles",
"keywords": [
"Los Angeles Roofing",
"Roofers in Los Angeles",
"HVAC Company in Los Angeles",
"Los Angeles HVAC Inspection"
]
},
{
"cityName": "Sacramento",
"keywords": [
"Sacramento Roofing",
"Roofers in Sacramento",
"HVAC Company in Sacramento",
"Sacramento HVAC Inspection"
]
},
]