2

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"}]
1
  • array.map(id => ({id})) Commented Mar 13, 2021 at 11:50

1 Answer 1

8

You can use Array#map as follows:

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

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

console.log(result);

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

1 Comment

This one is working, thanks for quick help @Majed Badawi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.