Home > Mobile >  I am trying to push some values from an object into a 3 dimensional array
I am trying to push some values from an object into a 3 dimensional array

Time:05-25

I'm trying to port an object over to an array. However once i get to constructedQuestionBank[0][1] I can no longer insert the information within Object.keys(conceptArray.Level1). It comes back with the error "Uncaught TypeError: constructedQuestionBank[0][1].push is not a function".

Could anyone support me in how to fix this. The reason i have chosen to place the information into an array is so that i can use random number generation to randomly select certain parts within the array. The tree needs to stay intact though. E.g (Level1 > Category1 > Item1 >text).

const conceptArray = {
    Level1: {
      Category1: {
        Item1: `text`,
        Item2: `text`,
      },

      Category2: {
        Item1: `text`,
        Item2: `text`,
      },
      Category3: {
        Item1: `text`,
        Item2: `text`,
      },
    },
      Level2: {
        Category1: {
          Item1: `text`,
          Item2: `text`,
        },
        Category2: {
          Item1: `text`,
          Item2: `text`,
        },
        Category3: {
          Item1: `text`,
          Item2: `text`,
        }
      }
}
    /*I want it to turn out something like this*/
    /*const exampleQuestionBank = [
        [`Level1`,
            [ `Category1`,
                [`Item1`,
                    [`text`]
                ],
                [`Item2`,
                    [`text`]
                ],
            ],
            [ `Category2`,
                [`Item1`,
                    [`text`]
                ],
                [`Item2`,
                    [`text`]
                ],
            ],
        ],
        [`Level2`,
            [ `Category1`,
                [`Item1`,
                    [`text`]
                ],
                [`Item2`,
                    [`text`]
                ],
            ],
            [ `Category2`,
                [`Item1`,
                    [`text`]
                ],
                [`Item2`,
                    [`text`]
                ],
            ],
        ],
    ]*/


    //What i have so far
    const constructedQuestionBank = [];
    const createQuestions = () => {
      constructedQuestionBank.push(Object.keys(conceptArray)); //inserts Levels

      Object.values(conceptArray).forEach(val => {
        constructedQuestionBank[0].push(Object.keys(val));
      }); //inserts Categories
      Object.values(conceptArray.Level1).forEach(val => {
        constructedQuestionBank[0][1].push(Object.keys(val));
      }); //Inserts Items 

      console.log(constructedQuestionBank)
    }
    createQuestions();

IMPORTANT: I should mention that Level1 Category1, Item1 etc are placeholders for ease of understanding. The real key names are just characters.

CodePudding user response:

The Object.entries() method converts an object to an array of key-value pairs. You can use this method recursively to convert all your nested levels into a multidimensional array.

function objectToArray(obj) {
    if (typeof obj !== 'object') {
      return obj
    }
    return Object.entries(obj).map(entry => [entry[0], ojectToArray(entry[1])])
}
console.log(objectToArray(conceptArray))

CodePudding user response:

Another option that may be a bit more performant and less error-prone would be to leave your data as an object, and use random number generation to key into the object instead.

So to randomly select one of the text elements you could generate 3 random numbers within the bounds of your options, and access it like this:

conceptArray[`Level${rand1}`][`Category${rand2}`][`Item${rand3}`]
  • Related