In the process of passing an object from a function containing an array named arrCombined
, I encountered a challenge with converting strings into integers. The goal is to map and remove these strings from an object titled results
so they can be converted into integers. However, I am currently facing an issue where I am getting undefined while mapping the array of objects for results
.
Below is a sample of the array:
0: Object { result: "494,927", risk: "LOW", sector: "Online" }
1: Object { result: "48,883", risk: "MEDIUM-LOW", sector: "Retail Stores" }
2: Object { result: "59,976", risk: "MEDIUM-LOW", sector: "Store Pick up" }
3: Object { result: "1,205,915", risk: "MEDIUM", sector: "Drive in" }
4: Object { result: "1,434,262", risk: "LOW", sector: "In store" }
To resolve this issue, I declared a variable called finalResult
within my mapping function to target the "result" value as follows:
let finalResult = arrCombined.arrCombined.result.map(function (e) {
return Number(e.replace(/(,\s*)+/g, '').trim());
});
console.log(finalResult); // undefined.
The expected outcome is for finalResult
to return the result objects as numbers, such as
494927, 48883, 59976, 1205915, 1434262