Skip to content
Advertisement

How to generate key value pair objects from an array in typescript

I have an array of ids and wanted to make an array object with adding a key “id” on it in typescript

array = ['6016b86edeccb2444cc78eef','6016b86edeccb2444cc78eee']

result = [{"id":"6016b86edeccb2444cc78eef"},{"id":"6016b878deccb2444cc78ef0"}]

Advertisement

Answer

You can use Array#map as follows:

const array = ['6016b86edeccb2444cc78eef','6016b86edeccb2444cc78eee'];

const result = array.map(id => ({id}));

console.log(result);
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement