Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert an array into object boolean [duplicate]

I have this array ["one","two","three"] would like to convert it into this {one:true, two:true, three:true}. Basically what am doing is converting the array items to keys of an object and make them boolean. I have tried using spread operator {... arr} which results to {0:'one',1:'two',2:'three'}, Any Ideas?

like image 990
G B Avatar asked Jun 29 '26 09:06

G B


1 Answers

You could map objects and assign them to a single object.

var array = ["one", "two", "three"],
    object = Object.assign(...array.map(k => ({ [k]: true })));

console.log(object);
like image 146
Nina Scholz Avatar answered Jun 30 '26 21:06

Nina Scholz