First ObjectArray, match key to second ObjectArray "state" value.
Second ObjectArray, append "title" and "color" key/values from first ObjectArray to second ObjectArray if the two letter state key on first ObjectArray matches the "state value on second ObjectArray.
Result should look like this third ObjectArray.
First ObjectArray, match key to second ObjectArray "state" value
{
"ChartOptions": [
{
"title": {
"CA": "California",
"GA": "Georgia",
"KY": "Kentucky",
"MN": "Minnesota",
"MO": "Missouri"
},
"color": {
"CA": "#6A5ACD",
"GA": "#FF0000",
"KY": "#0000FF",
"MN": "#FFA500",
"MO": "#3CB371"
}
}
]
}
Second ObjectArray, append "title" and "color" key/values from first ObjectArray to second ObjectArray if the two letter state key on first ObjectArray matches the "state value on second ObjectArray
{
"ChartSummary": [
{
"state": "CA",
"total": 812
},
{
"state": "GA",
"total": 240
},
{
"state": "KY",
"total": 167
},
{
"state": "MN",
"total": 578
},
{
"state": "MO",
"total": 75
}
]
}
Result should look like this third ObjectArray.
{
"ChartSummary": [
{
"state": "CA",
"total": 812,
"title": "California",
"color": "#6A5ACD"
},
{
"state": "GA",
"total": 240,
"title": "Georgia",
"color": "#FF0000"
},
{
"state": "KY",
"total": 167,
"title": "Kentucky",
"color": "#0000FF"
},
{
"state": "MN",
"total": 578,
"title": "Minnesota",
"color": "#FFA500"
},
{
"state": "MO",
"total": 75,
"title": "Missouri",
"color": "#3CB371"
}
]
}
2
Answers
Match Array Values
I wrote a little script that appends the colour and title properties from
ChartOptions
toChartSummary
when the state codes are matching. Also at the end you can check the result in the console since I’ve added aconsole.log(ChatSummary);
line.If you have any questions feel free to comment! I hope I could help you.
My Script:
This is the best way you can do it with
The two variables titles and colors are being defined to make the subsequent code more readable and efficient.
The ChartOptions array in firstObject contains objects which have keys for title and color, which are further objects mapping states to their corresponding titles and colors.
By defining:
You are essentially creating references to these nested objects so that you can use them more directly and avoid repeatedly accessing nested properties, which can be more computationally expensive.