What's the best way to convert a string array into an object? Is there any one line ES6 code not foreach function?
Array:
["FirstName", "MiddleName", "LastName"]
Expected object:
{ LastName: true, MiddleName: true, FirstName: true }
What's the best way to convert a string array into an object? Is there any one line ES6 code not foreach function?
Array:
["FirstName", "MiddleName", "LastName"]
Expected object:
{ LastName: true, MiddleName: true, FirstName: true }
You can do it using Array#Reduce
const arr = ["FirstName", "MiddleName", "LastName"]
const object = arr.reduce((acc, curr) => {
acc[curr] = true
return acc
}, {})
console.log(object)