数组长度与对象

时间:2018-05-09 16:34:57

标签: javascript typescript

我有跟随对象的数组:

我的测试数据:

  

const testData = [{'data':{'points':[{       'LIB':0.01,       '零':2.00}]}}]

我的班级:

class DataClass{


  constructor(lib, zero){
     this.lib = lib; 
     this.zero = zero;
  }

Here my function:

 createData(backendData) {
      const test = backendData[0]['data'].points
       const chartData = test.map((rf) => {
      return new DataClass(
        rf.lib,
        rf.zero)
    })
      return chartData;
}
}

let test = new DataClass();
let testA = test.createData(testData)
console.log(testA.length) ---> Here I am getting error..

解决方案:

我忘记了返回值。在我的情况下,chartData

1 个答案:

答案 0 :(得分:0)

在计算长度时,数组的内容无关紧要,只计算元素的数量。

所以,

const myArrayOfStrings = ["a", "b", "c", "d"]

const myArrayOfNumbers = [1, 2, 3 , 4]

const myArrayOfObjects = [{lib: 0.02, timo: 0.0794, zeroRateAnnualized: 0.03}, 
                          {lib: 0.06, timo: 0.0894, zeroRateAnnualized: 0.06}, 
                          {lib: 0.09, timo: 0.0994, zeroRateAnnualized: 0.08}, 
                          {lib: 0.29, timo: 0.1994, zeroRateAnnualized: 0.18}];

所有长度均为4

要查找数组的长度,请使用myArrayOfObjects.length

您尝试获取长度的数组不能存在: enter image description here