Here is an array resembling a JSON structure with n elements:
const mainData = [
{
phrase: "Phrase 1",
categorynumber: 1,
optionnumber: 1,
},
{
phrase: "Phrase 2",
categorynumber: 1,
optionnumber: 2,
},
{
phrase: "Phrase 3",
categorynumber: 2,
optionnumber: 1,
},
{
phrase: "Phrase 4",
categorynumber: 3,
optionnumber: 1,
},
{
phrase: "Phrase 5",
categorynumber: 3,
optionnumber: 1,
},
{
phrase: "Phrase 6",
categorynumber: 3,
optionnumber: 2,
},
];
The goal is to transform this into a nested Object structured as follows:
jsObject = {
1: {
1: ["Phrase 1"],
2: ["Phrase 2"],
},
2: {
1: ["Phrase 3"],
},
3: {
1: ["Phrase 4", "Phrase 5"],
2: ["Phrase 6"],
},
};
The data should be organized based on the categorynumber
and then by optionnumber
. (Pay attention to the format of "Phrase 4" and "Phrase 5".)