How to convert an array values to object keys in TypeScript

1 Answer

0 votes
const arr = ['typescript', 'c', 'c++'];
 
const obj = arr.reduce((accumulator, value) => {
    return {...accumulator, [value]: ''};
}, {});
 
console.log(obj);
 
   
   
   
   
/*
run:
   
{
  "typescript": "",
  "c": "",
  "c++": ""
} 
   
*/

 



answered May 23, 2022 by avibootz

Related questions

1 answer 288 views
1 answer 268 views
1 answer 220 views
2 answers 283 views
1 answer 261 views
1 answer 245 views
...