Map an Array of Arrays of Objects

63 Views Asked by At

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

2

There are 2 best solutions below

6
Abin Thaha On BEST ANSWER

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",
        },
    ],
];

const result = answerQuestionId.map(item => item.map(innerItem => innerItem.testo));
console.log(result);
console.log(result[0][0]);

2
G-Host On

The element you are accessing the attribute "testo" is an array itself.

try with this:

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

hopefully it will work