Map an Array of Arrays of Objects

I’m trying to map the text of a database of given asnwers in a List for my quiz App.
The problem is that i have to acces the title in an array of array of objects like this:
log of the database in console

I tried to map as usual using

console.log(answerQuestionId.map(element => `${element.testo}`));

but it returns an array of udefined. Can anyone help me with that? I want to get an array of arrays with only the text of the answers

>Solution :

I hope you are expecting an array of arrays with only testo data.

Check the code below.

answerQuestionId.map(item => item.map(innerItem => innerItem.testo))

const answerQuestionId = [
    [{
            corretta: false,
            domanda_id: 1,
            id: "1",
            immagine: false,
            testo: "Risposta_1",
        },
        {
            corretta: false,
            domanda_id: 1,
            id: "2",
            immagine: false,
            testo: "Risposta_2",
        },
        {
            corretta: false,
            domanda_id: 1,
            id: "3",
            immagine: false,
            testo: "Risposta_3",
        },
        {
            corretta: false,
            domanda_id: 1,
            id: "4",
            immagine: false,
            testo: "Risposta_4",
        },
    ],
    [{
            corretta: false,
            domanda_id: 2,
            id: "1",
            immagine: false,
            testo: "Two_1",
        },
        {
            corretta: false,
            domanda_id: 2,
            id: "2",
            immagine: false,
            testo: "Two_2",
        },
        {
            corretta: false,
            domanda_id: 2,
            id: "3",
            immagine: false,
            testo: "Two_3",
        },
        {
            corretta: false,
            domanda_id: 2,
            id: "4",
            immagine: false,
            testo: "Two_4",
        },
    ],
    [{
            corretta: false,
            domanda_id: 3,
            id: "1",
            immagine: false,
            testo: "Three_1",
        },
        {
            corretta: false,
            domanda_id: 3,
            id: "2",
            immagine: false,
            testo: "Three_2",
        },
        {
            corretta: false,
            domanda_id: 3,
            id: "3",
            immagine: false,
            testo: "Three_3",
        },
        {
            corretta: false,
            domanda_id: 3,
            id: "4",
            immagine: false,
            testo: "Three_4",
        },
    ],
    [{
            corretta: false,
            domanda_id: 4,
            id: "1",
            immagine: false,
            testo: "Four_1",
        },
        {
            corretta: false,
            domanda_id: 4,
            id: "2",
            immagine: false,
            testo: "Four_2",
        },
        {
            corretta: false,
            domanda_id: 4,
            id: "3",
            immagine: false,
            testo: "Four_3",
        },
        {
            corretta: false,
            domanda_id: 4,
            id: "4",
            immagine: false,
            testo: "Four_4",
        },
    ],
];

console.log(answerQuestionId.map(item => item.map(innerItem => innerItem.testo)));

Leave a Reply