0

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

5
  • what's the answerQuestionId? Commented Jun 27, 2023 at 9:11
  • I think you should remove the [0], try this one console.log(answerQuestionId.map(element => ${element.testo})); Commented Jun 27, 2023 at 9:11
  • @dan_pran oh Sorry, it's the name of the Array containing the arrays of objects. I obtained it doing this: let answerQuestionId = questions.map((q, a) => { let temp = answers.filter( (element) => ${element.domanda_id}` === ${q.id} ); return temp; });` where questions and answers were 2 arrays of objects and the answes had a field for the question_id Commented Jun 27, 2023 at 9:14
  • Please do not upload images of code/data/errors.; instead, please get into the habit of providing a proper minimal reproducible example when asking questions like this. Commented Jun 27, 2023 at 9:15
  • @AhmadFaraz I already tried and unfortunately it returns an array of undefined :( Commented Jun 27, 2023 at 9:16

2 Answers 2

0

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]);

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you so much! it works perfectly, i just had to change innerItem.testo to ${innerItem.testo}
@FabrizioMastrone, Happy to know that I was able to help you.. Happy coding.. :)
sorry if I keep bothering you, but once i have the array of arrays, how can i log for example just one element of one array? I'm trying console.log(Array[0][0]) but it gives me error TypeError: Cannot read properties of undefined (reading '0')
@FabrizioMastrone, yes you can access the 0th element of the 0th item in your array using array[0][0]. If you have stored the result in some variable, I don't think that would cause any error. I have edited my answer. Please check
I tried but it keep giving me Cannot read properties of undefined (reading '0') :(
|
0

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

2 Comments

ok I found out that if i keep the [0] in answerQuestionId[0] the map functions doesn't work. unfortunately if i try what you are suggesting i receive an error : TypeError: Cannot read properties of undefined (reading 'testo')
of course mine was a suggestion... not the "working code" as you wished. It was obvious you'd have to use an inner map() funtion to accomplish the result (as the accepted answer pointed out later). Anyway i'd suggest to change the data structure too, it's not very clear and difficult to manage in this way.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.