使所有对象的数组长度相同

时间:2016-12-09 03:01:49

标签: javascript arrays d3.js nvd3.js

我有一个如下所示的数据对象:

 [
  {
    "key": "k1204", 
    "values": [
      {
        "key": "Both", 
        "values": [
          {
            "key": "1420070400000", 
            "values": 27927.7349421797
          }, 
          {
            "key": "1422748800000", 
            "values": 27927.7349421797
          }, 
          {
            "key": "1425168000000", 
            "values": 27927.7349421797
          }, 
          {
            "key": "1427846400000", 
            "values": 27927.7349421797
          }, 
          {
            "key": "1430438400000", 
            "values": 27927.7349421797
          }, 
          {
            "key": "1433116800000", 
            "values": 27927.7349421797
          }, 
          {
            "key": "1435708800000", 
            "values": 27927.7349421797
          }, 
          {
            "key": "1438387200000", 
            "values": 27927.7349421797
          }, 
          {
            "key": "1441065600000", 
            "values": 27927.7349421797
          }, 
          {
            "key": "1443657600000", 
            "values": 27927.7349421797
          }, 
          {
            "key": "1446336000000", 
            "values": 36929.2001421797
          }, 
          {
            "key": "1448928000000", 
            "values": 36929.200142179696
          }, 
          {
            "key": "1451606400000", 
            "values": 36929.200142179696
          }, 
          {
            "key": "1454284800000", 
            "values": 36929.2001421797
          }, 
          {
            "key": "1456790400000", 
            "values": 36929.200142179696
          }, 
          {
            "key": "1459468800000", 
            "values": 36929.2001421797
          }, 
          {
            "key": "1462060800000", 
            "values": 36929.2001421797
          }, 
          {
            "key": "1464739200000", 
            "values": 36929.2001421797
          }, 
          {
            "key": "1467331200000", 
            "values": 36929.200142179696
          }, 
          {
            "key": "1470009600000", 
            "values": 36929.200142179696
          }, 
          {
            "key": "1472688000000", 
            "values": 36929.2001421797
          }, 
          {
            "key": "1488326400000", 
            "values": 36929.2001421797
          }
        ]
      }, 
      {
        "key": "Cloud", 
        "values": [
          {
            "key": "1420070400000", 
            "values": 289819.9054
          }, 
          {
            "key": "1422748800000", 
            "values": 289819.9054
          }, 
          {
            "key": "1425168000000", 
            "values": 289819.9054
          }, 
          {
            "key": "1427846400000", 
            "values": 289819.9054
          }, 
          {
            "key": "1430438400000", 
            "values": 289819.9054
          }, 
          {
            "key": "1433116800000", 
            "values": 289819.9054
          }, 
          {
            "key": "1435708800000", 
            "values": 289819.9054
          }, 
          {
            "key": "1438387200000", 
            "values": 289819.9054
          }, 
          {
            "key": "1441065600000", 
            "values": 289819.9054
          }, 
          {
            "key": "1443657600000", 
            "values": 289819.9054
          }, 
          {
            "key": "1446336000000", 
            "values": 289819.9054
          }, 
          {
            "key": "1448928000000", 
            "values": 289819.9054
          }, 
          {
            "key": "1451606400000", 
            "values": 289819.9054
          }, 
          {
            "key": "1454284800000", 
            "values": 289819.9054
          }, 
          {
            "key": "1456790400000", 
            "values": 289819.9054
          }, 
          {
            "key": "1459468800000", 
            "values": 289819.9054
          }, 
          {
            "key": "1462060800000", 
            "values": 289819.9054
          }, 
          {
            "key": "1464739200000", 
            "values": 289819.9054
          }, 
          {
            "key": "1467331200000", 
            "values": 366563.39859999996
          }, 
          {
            "key": "1470009600000", 
            "values": 366563.39859999996
          }, 
          {
            "key": "1472688000000", 
            "values": 366563.39859999996
          }, 
          {
            "key": "1475280000000", 
            "values": 366563.39859999996
          }, 
          {
            "key": "1477958400000", 
            "values": 366563.39859999996
          }, 
          {
            "key": "1480550400000", 
            "values": 366563.39859999996
          }, 
          {
            "key": "1483228800000", 
            "values": 366563.39859999996
          }, 
          {
            "key": "1485907200000", 
            "values": 366563.39859999996
          }, 
          {
            "key": "1488326400000", 
            "values": 366563.39859999996
          }, 
          {
            "key": "1491004800000", 
            "values": 375564.86380000005
          }, 
          {
            "key": "1493596800000", 
            "values": 375564.8638
          }, 
          {
            "key": "1496275200000", 
            "values": 375564.86380000005
          }, 
          {
            "key": "1498867200000", 
            "values": 375564.8638
          }, 
          {
            "key": "1501545600000", 
            "values": 375564.86380000005
          }, 
          {
            "key": "1504224000000", 
            "values": 375564.86380000005
          }, 
          {
            "key": "1506816000000", 
            "values": 375564.86380000005
          }, 
          {
            "key": "1509494400000", 
            "values": 375564.8638
          }, 
          {
            "key": "1512086400000", 
            "values": 375564.8638
          }, 
          {
            "key": "1514764800000", 
            "values": 499244.1814
          }, 
          {
            "key": "1517443200000", 
            "values": 499244.1814
          }, 
          {
            "key": "1519862400000", 
            "values": 499244.1814
          }, 
          {
            "key": "1522540800000", 
            "values": 499244.1814
          }, 
          {
            "key": "1525132800000", 
            "values": 499244.1814
          }, 
          {
            "key": "1527811200000", 
            "values": 499244.1814
          }, 
          {
            "key": "1530403200000", 
            "values": 499244.18139999994
          }, 
          {
            "key": "1533081600000", 
            "values": 499244.1814
          }, 
          {
            "key": "1535760000000", 
            "values": 499244.18139999994
          }, 
          {
            "key": "1538352000000", 
            "values": 499244.1814
          }, 
          {
            "key": "1541030400000", 
            "values": 499244.1814
          }, 
          {
            "key": "1543622400000", 
            "values": 499244.1814
          }, 
          {
            "key": "1546300800000", 
            "values": 499244.1814
          }, 
          {
            "key": "1548979200000", 
            "values": 499244.18139999994
          }, 
          {
            "key": "1551398400000", 
            "values": 499244.18139999994
          }, 
          {
            "key": "1554076800000", 
            "values": 499244.1814
          }, 
          {
            "key": "1556668800000", 
            "values": 499244.1814
          }, 
          {
            "key": "1559347200000", 
            "values": 499244.1814
          }, 
          {
            "key": "1561939200000", 
            "values": 499244.1814
          }, 
          {
            "key": "1564617600000", 
            "values": 499244.1814
          }, 
          {
            "key": "1567296000000", 
            "values": 499244.18139999994
          }, 
          {
            "key": "1569888000000", 
            "values": 499244.1814
          }, 
          {
            "key": "1572566400000", 
            "values": 499244.1814
          }, 
          {
            "key": "1575158400000", 
            "values": 499244.18139999994
          }
        ]
      }
    ]
  }, 
  {
    "key": "tre", 
    "values": [
      {
        "key": "On-Prem", 
        "values": [
          {
            "key": "1420070400000", 
            "values": 1602.24390394729
          }, 
          {
            "key": "1422748800000", 
            "values": 1602.24390394729
          }, 
          {
            "key": "1425168000000", 
            "values": 1602.24390394729
          }, 
          {
            "key": "1427846400000", 
            "values": 1602.24390394729
          }, 
          {
            "key": "1430438400000", 
            "values": 1602.24390394729
          }, 
          {
            "key": "1433116800000", 
            "values": 1602.24390394729
          }, 
          {
            "key": "1435708800000", 
            "values": 1602.24390394729
          }, 
          {
            "key": "1438387200000", 
            "values": 1602.24390394729
          }, 
          {
            "key": "1441065600000", 
            "values": 1602.24390394729
          }, 
          {
            "key": "1443657600000", 
            "values": 1602.24390394729
          }, 
          {
            "key": "1446336000000", 
            "values": 1602.24390394729
          }, 
          {
            "key": "1448928000000", 
            "values": 1602.24390394729
          }, 
          {
            "key": "1451606400000", 
            "values": 1602.24390394729
          }, 
          {
            "key": "1454284800000", 
            "values": 1602.24390394729
          }, 
          {
            "key": "1456790400000", 
            "values": 1602.24390394729
          }, 
          {
            "key": "1459468800000", 
            "values": 1602.24390394729
          }, 
          {
            "key": "1462060800000", 
            "values": 1602.24390394729
          }, 
          {
            "key": "1464739200000", 
            "values": 1602.24390394729
          }, 
          {
            "key": "1467331200000", 
            "values": 1602.24390394729
          }, 
          {
            "key": "1470009600000", 
            "values": 1602.24390394729
          }, 
          {
            "key": "1472688000000", 
            "values": 1602.24390394729
          }, 
          {
            "key": "1475280000000", 
            "values": 1602.24390394729
          }, 
          {
            "key": "1477958400000", 
            "values": 1602.24390394729
          }, 
          {
            "key": "1480550400000", 
            "values": 1602.24390394729
          }, 
          {
            "key": "1483228800000", 
            "values": 1602.24390394729
          }, 
          {
            "key": "1485907200000", 
            "values": 1602.24390394729
          }, 
          {
            "key": "1488326400000", 
            "values": 1602.24390394729
          }, 
          {
            "key": "1491004800000", 
            "values": 1602.24390394729
          }, 
          {
            "key": "1493596800000", 
            "values": 1602.24390394729
          }, 
          {
            "key": "1496275200000", 
            "values": 1602.24390394729
          }, 
          {
            "key": "1498867200000", 
            "values": 1602.24390394729
          }, 
          {
            "key": "1501545600000", 
            "values": 1602.24390394729
          }, 
          {
            "key": "1504224000000", 
            "values": 1602.24390394729
          }, 
          {
            "key": "1506816000000", 
            "values": 1602.24390394729
          }, 
          {
            "key": "1509494400000", 
            "values": 1602.24390394729
          }, 
          {
            "key": "1512086400000", 
            "values": 1602.24390394729
          }, 
          {
            "key": "1514764800000", 
            "values": 1602.24390394729
          }, 
          {
            "key": "1517443200000", 
            "values": 1602.24390394729
          }, 
          {
            "key": "1519862400000", 
            "values": 1602.24390394729
          }
        ]
      }, 
      {
        "key": "", 
        "values": [
          {
            "key": "1420070400000", 
            "values": 0
          }, 
          {
            "key": "1422748800000", 
            "values": 0
          }, 
          {
            "key": "1425168000000", 
            "values": 0
          },
          {
            "key": "1441065600000", 
            "values": 0
          }, 
          {
            "key": "1443657600000", 
            "values": 0
          }, 
          {
            "key": "1446336000000", 
            "values": 0
          }, 
          {
            "key": "1448928000000", 
            "values": 0
          }, 
          {
            "key": "1451606400000", 
            "values": 0
          }, 
          {
            "key": "1454284800000", 
            "values": 0
          }, 
          {
            "key": "1456790400000", 
            "values": 0
          }, 
          {
            "key": "1459468800000", 
            "values": 0
          }, 
          {
            "key": "1462060800000", 
            "values": 0
          }, 
          {
            "key": "1464739200000", 
            "values": 0
          }, 
          {
            "key": "1467331200000", 
            "values": 0
          }, 
          {
            "key": "1470009600000", 
            "values": 0
          }, 
          {
            "key": "1472688000000", 
            "values": 0
          }, 
          {
            "key": "1475280000000", 
            "values": 0
          }, 
          {
            "key": "1477958400000", 
            "values": 0
          }, 
          {
            "key": "1480550400000", 
            "values": 0
          }, 
          {
            "key": "1483228800000", 
            "values": 0
          }, 
          {
            "key": "1485907200000", 
            "values": 0
          }, 
          {
            "key": "1488326400000", 
            "values": 0
          }, 
          {
            "key": "1491004800000", 
            "values": 0
          }, 
          {
            "key": "1493596800000", 
            "values": 0
          }, 
          {
            "key": "1496275200000", 
            "values": 0
          }, 
          {
            "key": "1498867200000", 
            "values": 0
          }, 
          {
            "key": "1501545600000", 
            "values": 0
          }, 
          {
            "key": "1504224000000", 
            "values": 0
          }, 
          {
            "key": "1506816000000", 
            "values": 0
          }, 
          {
            "key": "1509494400000", 
            "values": 0
          }, 
          {
            "key": "1512086400000", 
            "values": 0
          }, 
          {
            "key": "1514764800000", 
            "values": 0
          }, 
          {
            "key": "1517443200000", 
            "values": 0
          }, 
          {
            "key": "1519862400000", 
            "values": 0
          }, 
          {
            "key": "1522540800000", 
            "values": 0
          }, 
          {
            "key": "1525132800000", 
            "values": 0
          }, 
          {
            "key": "1527811200000", 
            "values": 0
          }, 
          {
            "key": "1530403200000", 
            "values": 0
          }, 
          {
            "key": "1533081600000", 
            "values": 0
          }, 
          {
            "key": "1535760000000", 
            "values": 0
          }, 
          {
            "key": "1538352000000", 
            "values": 0
          }, 
          {
            "key": "1541030400000", 
            "values": 0
          }, 
          {
            "key": "1543622400000", 
            "values": 0
          }, 
          {
            "key": "1546300800000", 
            "values": 0
          }, 
          {
            "key": "1548979200000", 
            "values": 0
          }, 
          {
            "key": "1551398400000", 
            "values": 0
          }, 
          {
            "key": "1554076800000", 
            "values": 0
          }, 
          {
            "key": "1556668800000", 
            "values": 0
          }, 
          {
            "key": "1559347200000", 
            "values": 0
          }, 
          {
            "key": "1561939200000", 
            "values": 0
          }
        ]
      }, 
      {
        "key": "Cloud", 
        "values": [
          {
            "key": "1420070400000", 
            "values": 104732.0298
          }, 
          {
            "key": "1422748800000", 
            "values": 104732.0298
          }, 
          {
            "key": "1425168000000", 
            "values": 104732.0298
          }, 
          {
            "key": "1427846400000", 
            "values": 104732.0298
          }, 
          {
            "key": "1430438400000", 
            "values": 104732.0298
          }, 
          {
            "key": "1433116800000", 
            "values": 104732.0298
          }, 
          {
            "key": "1435708800000", 
            "values": 104732.0298
          }, 
          {
            "key": "1438387200000", 
            "values": 104732.0298
          }, 
          {
            "key": "1441065600000", 
            "values": 104732.0298
          }, 
          {
            "key": "1443657600000", 
            "values": 104732.0298
          }, 
          {
            "key": "1446336000000", 
            "values": 104732.0298
          }, 
          {
            "key": "1448928000000", 
            "values": 104732.0298
          }, 
          {
            "key": "1451606400000", 
            "values": 104732.0298
          }, 
          {
            "key": "1454284800000", 
            "values": 104732.0298
          }, 
          {
            "key": "1456790400000", 
            "values": 104732.0298
          }, 
          {
            "key": "1459468800000", 
            "values": 104732.0298
          }, 
          {
            "key": "1462060800000", 
            "values": 104732.0298
          }, 
          {
            "key": "1464739200000", 
            "values": 104732.0298
          }, 
          {
            "key": "1467331200000", 
            "values": 104732.0298
          }, 
          {
            "key": "1470009600000", 
            "values": 104732.0298
          }, 
          {
            "key": "1472688000000", 
            "values": 104732.0298
          }, 
          {
            "key": "1475280000000", 
            "values": 104732.0298
          }, 
          {
            "key": "1477958400000", 
            "values": 104732.0298
          }, 
          {
            "key": "1480550400000", 
            "values": 181475.523
          }, 
          {
            "key": "1483228800000", 
            "values": 181475.523
          }, 
          {
            "key": "1485907200000", 
            "values": 181475.523
          }, 
          {
            "key": "1488326400000", 
            "values": 181475.523
          }, 
          {
            "key": "1491004800000", 
            "values": 181475.523
          }, 
          {
            "key": "1493596800000", 
            "values": 181475.523
          }, 
          {
            "key": "1496275200000", 
            "values": 181475.523
          }, 
          {
            "key": "1498867200000", 
            "values": 181475.523
          }, 
          {
            "key": "1501545600000", 
            "values": 181475.523
          }, 
          {
            "key": "1504224000000", 
            "values": 181475.523
          }, 
          {
            "key": "1506816000000", 
            "values": 181475.523
          }, 
          {
            "key": "1509494400000", 
            "values": 181475.523
          }, 
          {
            "key": "1512086400000", 
            "values": 181475.523
          }, 
          {
            "key": "1514764800000", 
            "values": 181475.523
          }, 
          {
            "key": "1517443200000", 
            "values": 181475.523
          }, 
          {
            "key": "1519862400000", 
            "values": 181475.523
          }, 
          {
            "key": "1522540800000", 
            "values": 181475.523
          }, 
          {
            "key": "1525132800000", 
            "values": 181475.523
          }, 
          {
            "key": "1527811200000", 
            "values": 181475.523
          }, 
          {
            "key": "1530403200000", 
            "values": 181475.523
          }, 
          {
            "key": "1533081600000", 
            "values": 181475.523
          }, 
          {
            "key": "1535760000000", 
            "values": 181475.523
          }, 
          {
            "key": "1538352000000", 
            "values": 181475.523
          }, 
          {
            "key": "1541030400000", 
            "values": 181475.523
          }, 
          {
            "key": "1543622400000", 
            "values": 181475.523
          }, 
          {
            "key": "1546300800000", 
            "values": 181475.523
          }, 
          {
            "key": "1548979200000", 
            "values": 181475.523
          }, 
          {
            "key": "1551398400000", 
            "values": 181475.523
          }, 
          {
            "key": "1554076800000", 
            "values": 181475.523
          }, 
          {
            "key": "1556668800000", 
            "values": 181475.523
          }
        ]
      }
    ]
  }
]

我需要确保key, values数组的长度相同,以便我可以使用nvd3在stackedAreaChart上绘制它们,因为我需要遵循两个条件...

  1. 系列应具有相同的时间范围(范围)。
  2. 系列长度,即'值'数据中的所有对象的数组长度应该相同。
  3. 我试过跟随这个fidde:http://jsfiddle.net/krispo1/nR6qN/2/

    在控制器中执行此操作:

    get_scn_cst_data().then(function(costByScn){
    
                    var data2 = []
    
                    for(i = 0; i < costByScn.length; i++) {
                        console.log("i:", JSON.stringify(costByScn[i].values))
                        var data = costByScn[i].values;
    
                        if(data && Array.isArray(data)) { 
                                console.log("is array")
                                }
    
                        data = data.map(function(series){
                                  series.values = series.values.map(function(d,i){
                                      return [data[2].values[i][0], d[1]]
                                  })
                                  return series;
                              });
    
                    }
    
                    console.log("data2: ", JSON.stringify(costByScn));
    
                    $scope.data_costByScn =   costByScn;
    
    })
    

    但收到此错误:TypeError: data.map is not a function

    ---- ----- EDIT

    进行了更改以修复数组,但现在在此部分的Cannot read property 'values' of undefined处收到此错误:

                    data = data.map(function(series){
                              series.values = series.values.map(function(d,i){
                                  return [data[2].values[i][0], d[1]]
                              })
                              return series;
                          });
    

    为什么价值回归未定义?当我控制日志数据时,它看起来是一个values

    的数组

1 个答案:

答案 0 :(得分:2)

只是类型错误意味着你试图在不是数组的东西上调用.map!

在将数据发送到.map之前,请确认它是有效的数组

您可以使用:if(data && Array.isArray(data)) { // your login goes here... }