I am trying to convert an array of objects to objects with reduce method. The problem is I want to have object keys in numeric.
let crops = [{
id: 1,
name: "wheat"
}, {
id: 2,
name: "rice"
}];
let cropsObj = crops.reduce((accumulator, currentValue) => {
accumulator[currentValue.id] = currentValue.name
return accumulator;
}, {});
console.log(cropsObj);
This works fine except the keys I am getting are strings. For example:
{"1":"wheat","2":"rice"}
What I want is {1:"wheat",2:"rice"}. How can I convert the keys to integers?
Map...{"1":"wheat","2":"rice"}and{1:"wheat",2:"rice"}are same thing, JS internally converts them to string