This question is similar to one of my previous questions (removing objects in an object in an array javascript). However instead of having only numbers, there are also complex numbers with "NaN" as the real values. I thought this could be happening since I'm using big numbers and Mathjs, but all of my other values are already floated to ~15 decimal places. The returned array froma console.log(realPowers) looks like this in the console log:
0:0
1:91.51069578156118
2:183.02210760273937
3:277.73380284266796
4:376.47588381083307
5:481.85928667762994
6:599.6533611633058
7:752.8633763048005
8:Complex {re: NaN, im: -0.015021590179814361}
9:Complex {re: NaN, im: -0.029563247908981544}
10:Complex {re: NaN, im: -0.047829041780228475}
This is interesting since the first line of code below should return only the real values of the complex numbers. I am trying to feed these values into a for loop using the toFixed method to reduce the number of decimal places:
var realPowers = results.totalPower.map((x) => x && x.re ? x.re :x);
function fixPowers(realPowers) {
var k,fixedPower,powerToFix
for (k=0,fixedRealPowers=[]; k < realPowers.length; k++) {
powerToFix = realPowers[k];
fixedPower = powerToFix.toFixed(3);
fixedRealPowers.push(fixedPower);
}
return fixedRealPowers
};
Where totalPower is the original array. But when I do that it returns an error:
powerToFix.toFixed is not a function
since toFixed cannot be used on a string (I'm assuming).
I am trying to make an array that looks like this:
0:0
1:91.51069578156118
2:183.02210760273937
3:277.73380284266796
4:376.47588381083307
5:481.85928667762994
6:599.6533611633058
7:752.8633763048005
8:NaN
9:NaN
10:NaN
Can I reduce the number of decimals for the real numbers and keep only the NaN part of the complex numbers using this same or similar method?