Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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)));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading