I have some dynamic data. Assume that id will always be present, and the other key:values will change.
I am trying to wrap my head around an abstract approach, but I don't think I know enough about the tools I have available.
I am looking for an approach to aggregate dynamically.
example, if I have this data:
const dataum = [{
"id": 1,
"q1": "yes",
"q2": 4,
"q3": null
}, {
"id": 2,
"q1": null,
"q2": 14,
"q3": "medium"
}, {
"id": 3,
"q1": "no",
"q2": 83,
"q3": "high"
}, {
"id": 27,
"q1": "yes",
"q2": 62,
"q3": null
}]
and am attempting to create something like this:
const aggData = [{
"q1": {
"yes": 2,
"empty": 1,
"no": 1
}
}, {
"q2": {
"0to60": 2,
"60to70": 1,
"70to100": 1,
"empty": 0
}
} {
"q3": {
"medium": 1,
"empty": 2,
"high": 1
}
}]
I haven't tried much. because I am stuck at the outer most process / function. I have thought about for() .keys and .values, I have looked at array.reduce()... but I still do not get how to be able to dynamically say: newObject.key.someAnswer += 1 where the key is dynamic, and the answer is dynamic (or null).
I wouldn't worry too much about the ranges... I think with a tiny bit of help I can fork the process.
I can provide some more data to this process, in any format, like:
const qProperties = [{
"q1": {
"type": "YN",
"values": ["yes", "no"]
}
}, {
"q3": {
"type": "LMH",
"values": ["low", "medium", "high"]
}
}]
If it would help reduce the code footprint, make it more abstract.