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

...