How to convert an array values to object keys in JavaScript

1 Answer

0 votes
const arr = ['javascript', 'c', 'c++'];

const obj = arr.reduce((accumulator, value) => {
  	return {...accumulator, [value]: ''};
}, {});

console.log(obj);

  
  
  
  
/*
run:
  
{
  c: "",
  c++: "",
  javascript: ""
}
  
*/

 



answered May 23, 2022 by avibootz

Related questions

...