I am trying to convert array to objects .
let arr = ["value1","value2"]
My trying code is :
Object.assign({},arr)
expected Output :
{value1:{},value2:{} }
You can try with .forEach() as the following:
const arr = ["value1", "value2"];
const result = {};
arr.forEach(e => result[e] = {});
console.log(result);
You could take Object.fromEntries and map the wanted keys with empty objects.
let keys = ["value1", "value2"],
object = Object.fromEntries(keys.map(key => [key, {}]));
console.log(object);
Use array reduce!
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
arr.reduce((accumulator, value) => ({ ...accumulator, [value]: {} }), {});
You can use .reduce() to get the desired output:
const data = ["value1", "value2"];
const result = data.reduce((r, k) => (r[k] = {}, r), {});
console.log(result);