Search code examples
typescriptionic4associative-array

saving multiple string labels and array to associated array in typescript


I am building an ionic 4 app which has to save multiple labels and arrays to an associative array but having trouble with initializing and pushing new items to the array and editing/updating existing array items in the associative array.

  array1 = {
           'Question1'=>['True','True'],
           'Question2'=>['False, False'] 
           }

How can I do this in typescript?


Solution

  • JavaScript/TypeScript does not support arrays with named indexes, arrays always use numbered indexes. You can, however, use a simple object to do this, e.g.:

        const questions = {
            "question1": [true, true],
            "question2": [false, false]
        }
        
        console.log(questions);
        console.log(questions["question1"]) // prints [true, true]