I'm working on a reactjs web app I have an array like
let arr = ["name","message", etc...];
I want to convert it to object but to look like
let desired = { name:'', message:'' };
I tried a few things but none of them worked.
If you just want to assign each value in the array the value of an empty string, use reduce:
let arr = ["name","message", "etc"];
let desired = arr.reduce((acc, curr) => (acc[curr] = "", acc), {});
console.log(desired);
.as-console-wrapper { max-height: 100% !important; top: auto; }
ES5 syntax:
var arr = ["name","message", "etc"];
var desired = arr.reduce(function(acc, curr) {
acc[curr] = "";
return acc;
}, {});
console.log(desired);
.as-console-wrapper { max-height: 100% !important; top: auto; }
I tried few things but none worked? can you show code you have tried so far ?