I want to map the java script array into dictionary:
let myArray=['first','second','third'];
Expected Output
result={first:1,second:1,third:1}
Actual Output
result=[{element:1}, {element:1}, {element:1}]
Code:
let myArray=['first','second','third'];
let result=myArray.map(element=>{
return {element:1}
})
You could do this with Object.assign
and spread syntax.
let myArray=['first','second','third'];
let obj = Object.assign({}, ...myArray.map(key => ({[key]: 1})));
console.log(obj)
let myArray=['first','second','third'];
let result = myArray.reduce((agg, ele) => {
agg[ele] = 1;
return agg;
}, {});
console.log(result);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With