I have a questions object with nested objects:
const questions = {
QID5: {
choices: {
1: { choiceText: "Label 1" },
2: { choiceText: "Label 2" },
3: { choiceText: "Label 3" }
}
},
QID6: {
choices: {
1: { choiceText: "English" },
2: { choiceText: "French" }
}
}
};
And an array of responses:
const responses = [
{
labels: {
QID5: ["Label 1", "Label 3"],
QID6: ["English", "French"]
}
},
{
labels: {
QID5: ["Label 1"],
QID6: ["English"]
}
},
{
labels: {
QID5: ["Label 2", "Label 3"],
QID6: ["English"]
}
}
];
I want to count the occurrence of each choice in questions in each of the responses for a particular question (QID) and put them in arrays as a property (x, y) of a new object
For example the desired result for QID5 should be:
const desiredResult = {
id: "QID5",
x: ["Label 1", "Label 2", "Label 3"],
y: [2, 1, 2]
};
Label 1 in desiredResult.x gets the value 2 in desiredResult.y (because it appears in two of the responses) and so on...
How can I achieve this result?
Here is what I've tried:
function getResults(id) {
return responses.flatMap(response => response.labels[id])
}
const arr = getResults("QID5");
const counts = {};
arr.forEach((x) => {
counts[x] = (counts[x] || 0) + 1;
});
const x = Object.keys(counts)
const y = Object.values(counts)
console.log(x)
console.log(y)