philly2013
philly2013

Reputation: 157

Modify array in typescript/angular

I am trying to change an array of strings to an array of key-value pairs as shown below.
["a","b"] becomes [{"Info":"a"},{"Info":"b"}]

Any advice will be great.

Upvotes: 0

Views: 1209

Answers (2)

developer033
developer033

Reputation: 24864

You can use map:

const result = ['a', 'b'].map(item => ({ info: item });

Upvotes: 3

Saravana
Saravana

Reputation: 40544

Use Array#map:

let array = ["a", "b"];

let mappedArray: Array<{
    info: string
}> = array.map(item: string => {
    return {
        info: item
    };
})

Upvotes: 0

Related Questions