Currently, I am retrieving data from a MySQL database and converting it to JSON before passing it to a JavaScript class for chart display purposes. The challenge lies in creating arrays required by the chart from the JSON object without manually creating and populating them.
Here is what my object structure looks like:
var myJSON = [
{"name1": "value11", "name2":"value21", "name3": "value31"},
{"name1": "value12", "name2":"value22", "name3": "value32"},
{"name1": "value13", "name2":"value23", "name3": "value33"}
]
I aim to transform it into this format:
var myData = {
"name1": ["value11", "value12", "value13"],
"name2": ["value21", "value22", "value23"],
"name3": ["value31", "value32", "value33"],
}
While I could manually create and populate the arrays, I believe there must be a more elegant solution. I have explored functions like Map, Object.keys, and Object.entries but haven't been able to crack the code.
My preference is to resolve this issue within JavaScript, but I've also included Mysql/PDO tags as alternative data fetching methods are on the table.
Despite my efforts since Friday, an elegant solution continues to elude me. Any assistance would be greatly valued.