为什么我的数组 reduce 函数返回 NaN?

时间:2021-04-01 15:03:56

标签: arrays angular typescript reduce

我有两个数组,一个数组在包含随机数 yIsGiven() 的数组中只有像 y 这样的属性和一个数组,它等于第一个数组,但有一个区别,这次我所有的属性是y,除了一个,还有一个z,还有随机数zIsGiven()

我的目标是将它们全部计算在一起,在我的第一个数组上它运行良好,但在我的第二个数组中它不起作用仅仅因为我将一个 y 属性更改为 z 并且我得到 NaN.

我想对数组中的所有值求和,我如何使用 zIsGiven() 执行此操作?

我的 app.component.html:

<h4>My array is in total:</h4>
<p>{{yIsGiven()}}</p>

<h4>But if I change my attribute from y to z I get:</h4>
<p>{{zIsGiven()}}</p>

我的 app.component.ts:

yIsGiven() {
    var temp = [
      {
        name: "Agency",
        y: 110, /* All attributes are y */
        drilldown: {
          name: "Agency",
          categories: ["APPS & SI", "ERS"],
          data: [24, 8]
        }
      },
      {
        name: "ER",
        y: 60,
        drilldown: {
          name: "ER",
          categories: ["APPS & SI", "ERS"],
          data: [7, 53]
        }
      },
      {
        name: "Direct",
        y: 60,
        drilldown: {
          name: "Direct",
          categories: ["APPS & SI", "ERS"],
          data: [31, 29]
        }
      }
    ];

    var reduced = temp
      .map(function(obj) {
        return obj.y;
      })
      .reduce(function(a, b) {
        return a + b;
      });

    return reduced;
  }

  zIsGiven() {
    var temp = [
      {
        name: "Agency",
        z: 110 /* For example this is now z and not y */,
        drilldown: {
          name: "Agency",
          categories: ["APPS & SI", "ERS"],
          data: [24, 8]
        }
      },
      {
        name: "ER",
        y: 60,
        drilldown: {
          name: "ER",
          categories: ["APPS & SI", "ERS"],
          data: [7, 53]
        }
      },
      {
        name: "Direct",
        y: 60,
        drilldown: {
          name: "Direct",
          categories: ["APPS & SI", "ERS"],
          data: [31, 29]
        }
      }
    ];

    var reduced = temp
      .map(function(obj) {
        return obj.y;
      })
      .reduce(function(a, b) {
        return a + b;
      });

    return reduced;
  }

如果我只更改一个值,我就无法对数组中的所有值求和,但是如果我想对数组中的不同属性求和怎么办?它只是不适用于 array reduce,或者我就是想不通。

感谢您的帮助,谢谢!

工作: https://stackblitz.com/edit/angular-ivy-tanetr?file=src%2Fapp%2Fapp.component.html

最好的问候

3 个答案:

答案 0 :(得分:1)

数组中的一个变量的 y 属性未定义,您可以这样做:

var reduced = temp
  .map(function(obj) {
    return obj.y;
  })
  .reduce(function(a, b) {
    if (b === undefined) return a;
    return a + b;
  }, 0);

修复错误。

答案 1 :(得分:0)

将 zIsGiven 更改为:

    zIsGiven() {
    var temp = [
      {
        name: "Agency",
        z: 110 /* For example this is now z and not y */,
        drilldown: {
          name: "Agency",
          categories: ["APPS & SI", "ERS"],
          data: [24, 8]
        }
      },
      {
        name: "ER",
        z: 60,
        drilldown: {
          name: "ER",
          categories: ["APPS & SI", "ERS"],
          data: [7, 53]
        }
      },
      {
        name: "Direct",
        z: 60,
        drilldown: {
          name: "Direct",
          categories: ["APPS & SI", "ERS"],
          data: [31, 29]
        }
      }
    ];

    var reduced = temp
      .map(function(obj) {
        return obj.z;
      })
      .reduce(function(a, b) {
        return a + b;
      });

    return reduced;
  }

答案 2 :(得分:0)

如果您查看对象定义,第一个具有 z 属性,其他具有 y 属性。

因为一个是 undefined(当你请求 obj.yundefined 加上一个数字给你 NaN

您可以在注释掉 console.log(reduced)

后通过 .reduce() 检查
相关问题