仅显示复杂数组javascript的实际价值 [英] only display real value from complex array javascript

查看:68
本文介绍了仅显示复杂数组javascript的实际价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题与我之前的问题之一(

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}

这很有趣,因为下面的第一行代码应仅返回复数的实数值.我正在尝试使用toFixed方法将这些值输入for循环中,以减少小数位数:

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
};

totalPower是原始数组.但是当我这样做时,它会返回错误:

Where totalPower is the original array. But when I do that it returns an error:

powerToFix.toFixed is not a function

因为toFixed不能在字符串上使用(我假设是这样).

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

我可以使用相同或相似的方法减少实数的小数位数并仅保留复数的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?

推荐答案

问题在于Java中NaN被认为是错误的,因此对于带有re: NaN的复数,x && x.refalsey.然后,它返回x,它是整个复数.如果要测试属性是否存在,请使用hasOwnProperty()方法.

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());

这篇关于仅显示复杂数组javascript的实际价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆