StackOverflow - StackOverflow

How to perform a forEach loop on a variable defied as string | Array<string>?

if (Array.isArray(question.answer)) {
    (question.answer as Array<string>).forEach(...)
}

also you can do this to have a uniform code

    let answers : Array<string> = [];
    if (Array.isArray(question.answer)) {
        answers = [...question.answer];
    } else if (!!question.answer){
        answers = [question.answer];
    }

    answers.forEach(answer => ....)

Was this helpful?

Have a different question?

Can't find the answer you're looking for? Submit your own question to our community.

🛎️ Get Weekly OTA Fixes

New answers, vendor issues, and updates — straight to your inbox.