I’ve got an array of objects with a targeted field containing a string value that could be greater or less than a billion. When I loop through and push the strings to my array: number[], values over a billion magically become values like 5. Hundreds of millions work fine. I’ve tried a several strategies (BigInt, if/else to sort on value, etc.), but I’m missing something dumb here. Here is the code block:
(response) => {
response.forEach(element => {
this.valueArray.push(parseInt(element.value))
each element is an object from an api like { hamburgers: ‘7’, hotdogs: ‘9’, feelings: ‘strong’ value: ‘9000000000’}
The valueArray is being used to populate a chart.js barchart, and if there are values being converted from 1,000,000,001 to 1, the chart looks crazy.
Thanks in advance!
I thought this might work but it doesn’t behave like I thought it would and return NaN:
` if(element.value.length < 10) {
this.value.push(Number(element.value))
return;
}
else if(element.value.length > 9) {
this.value.push(Number(BigInt(element.value)))
}
break;`
Doug Rosenberg is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.