I have this array
var labels = ["Hat", "Chair", "Pen"];
and I want the output
var output = {"Hat": true, "Chair": true, "Pen": true};
how to convert this in javascript
Thanks
I have this array
var labels = ["Hat", "Chair", "Pen"];
and I want the output
var output = {"Hat": true, "Chair": true, "Pen": true};
how to convert this in javascript
Thanks
You can use .reduce
:
var labels = ["Hat", "Chair", "Pen"];
const obj = labels.reduce((acc,e) => {
acc[e] = true;
return acc;
}, {});
console.log(obj);
var labels = ["Hat", "Chair", "Pen"];
let labelsObject = {};
labels.forEach(label => {
labelsObject[label] = true;
})
console.log(labelsObject)