I am currently developing a unique keyword generation tool for my application. The tool takes a list of cities and organizes them into keyword groups based on specific categories.
So far, I have experimented with different methods such as using .map
on the state and .find
to identify matches. However, I am encountering difficulties in properly merging the data to achieve the desired end result. Any guidance or suggestions on how to accomplish this 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;
}
}
};
When triggering an action, the input looks something like:
[
{
cityName: "Los Angeles",
keywords: ["Los Angeles HVAC Repair", "HVAC Installation Los Angeles"]
},
{
cityName: "Sacramento",
keywords: ["Sacramento HVAC Repair", "HVAC Installation Sacramento"]
}
]
The initial action works fine, but subsequent actions do not merge objects with the same cityName
. Instead, they produce:
[
{
"cityName": "Los Angeles",
"keywords": [
[
"Los Angeles Roofing",
"Roofers in Los Angeles"
]
]
},
{
"cityName": "Sacramento",
"keywords"&: [
[
"Sacramento Roofing",
"Roofers in Sacramento"
]
]
},
...
]
Desired output is shown below:
[
{
"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"
]
}
]