only display real value from complex array javascr

2019-07-31 15:40发布

问题:

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?

回答1:

The problem is that NaN is considered falsey in Javascript, so x && x.re is falsey for the complex numbers with re: NaN. It then returns x, which is the whole complex number. If you want to test whether a property exists, use the hasOwnProperty() method.

function Complex(real, imag) {
  return {re: real, im: imag};
}
var results = {
  totalPower: [
    0,
    91.51069578156118,
    183.02210760273937,
    277.73380284266796,
    376.47588381083307,
    481.85928667762994,
    599.6533611633058,
    752.8633763048005,
    Complex(NaN, -0.015021590179814361),
    Complex(NaN, -0.029563247908981544),
    Complex(NaN, -0.047829041780228475)
  ]
};

var realPowers = results.totalPower.map((x) => x && x.hasOwnProperty('re') ? x.re :x);

function fixPowers() {

  var k,fixedPower,powerToFix

  for (k=0,fixedRealPowers=[]; k < realPowers.length; k++) {
    powerToFix = realPowers[k];
    fixedPower = powerToFix.toFixed(3);
    fixedRealPowers.push(fixedPower);
  }
  return fixedRealPowers
};

console.log(fixPowers());



回答2:

var realPowers = results.totalPower.map((x) => x && x.re ? x.re :x);

function fixPowers(totalPower) {

  var k,fixedPower,powerToFix

  for (k=0,fixedRealPowers=[]; k < realPowers.length; k++) {
    powerToFix = realPowers[k];

    fixedPower = isNaN(powerToFix) ? powerToFix : powerToFix.toFixed(3);
    fixedRealPowers.push(fixedPower);
  }
  return fixedRealPowers
};