循环最终无意中将数千个对象推入阵列

时间:2016-12-12 18:40:08

标签: javascript arrays

让我们说我的对象有这样的结构:

var costByScn = [
  {
    "key": "K1204", 
    "values": [
      {
        "key": "Both", 
        "values": [
          {
            "key": "1420070400000", 
            "values": 27927.7349421797
          }, 
          ...
        ]
      }, 
      ...
    ]
  }, 
  {
    "key": "tere", 
    "values": [
      {
        "key": "On-Prem", 
        "values": [
          {
            "key": "1438387200000", 
            "values": 1602.24390394729
          }, 
          ...
        ]
      }, 
      ...
    ]
  }
]

我想要做的是找到最小和最大日期时间值,然后遍历所有数组,基本上"同步"他们在一起。

因此,如果我的分钟是1420070400000且我的最大值是1575158400000(按月增加),那么它应该创建任何缺失的对,values为0.

现在,我基本上已经有了一堆循环来尝试这样做(如果有更有效的方法,我会很好奇。)

我现在要做的是创建和dtRng个时间戳,以毫秒为单位,在dtMindtMax之间按月递增,然后确保每个增量在数组中有一个对象。

我想我已经接近了,但是现在在这个部分scnFtprntDates.values.push({key: dtRng[q], values: 0}) ...它似乎正在推动成千上万的新对象进入阵列并且不断崩溃浏览器我无法做到似乎弄明白了为什么。它看起来像一个无限循环,但无法弄清楚控制台日志显示的内容:

dtMin: 1420070400000
dtMax: 1575158400000 
dtRng: [1420070400000, 1422748800000, 1425168000000, 1427842800000, 1430434800000, 1433113200000, 1435705200000, 1438383600000, 1441062000000, 1443654000000, 1446332400000, 1448928000000, 1451606400000, 1454284800000, 1456790400000, 1459465200000, 1462057200000, 1464735600000, 1467327600000, 1470006000000, 1472684400000, 1475276400000, 1477954800000, 1480550400000, 1483228800000, 1485907200000, 1488326400000, 1491001200000, 1493593200000, 1496271600000, 1498863600000, 1501542000000, 1504220400000, 1506812400000, 1509490800000, 1512086400000, 1514764800000, 1517443200000, 1519862400000, 1522537200000, 1525129200000, 1527807600000, 1530399600000, 1533078000000, 1535756400000, 1538348400000, 1541026800000, 1543622400000, 1546300800000, 1548979200000, 1551398400000, 1554073200000, 1556665200000, 1559343600000, 1561935600000, 1564614000000, 1567292400000, 1569884400000, 1572562800000, 1575158400000]

这是我的代码:

var scn = costByScn

for (var i=0; i<scn.length; i++) {

    var dtMax = 0;
    var dtMin = 1923273544000;


    var scnFtprnt = scn[i];

    console.log("scnftprnt: ", scnFtprnt);

    console.log("scnftprnt: ", scnFtprnt.values.length);

    for (j=0; j<scnFtprnt.values.length; j++) {

        var scnFtprntDates = scnFtprnt.values[j];

        for (var q=0; q<scnFtprntDates.values.length; q++) {

            var scnFtprntDt = scnFtprntDates.values[q].key

            if (dtMax < scnFtprntDt) {

                dtMax = scnFtprntDt
            }

            if (dtMin > scnFtprntDt) {

                dtMin = scnFtprntDt 
            }


        }

    }

   var dtRng = []

    console.log("dtmin: ", dtMin);
   console.log("dtmax: ", dtMax);

    var offset = 5*60*60000

    dtMin = new Date(+dtMin + offset);
    dtMax = new Date(+dtMax + offset);

   console.log("dtmin: ", dtMin);
   console.log("dtmax: ", dtMax);

   while (dtMin <= dtMax) {

       dtRng.push(dtMin.getTime() - offset);

       dtMin = new Date(new Date(dtMin).setMonth(dtMin.getMonth()+1));

   }

    console.log("dt rng:", dtRng);

    console.log("dt rng:", dtRng.length);

    for (l=0; l<scnFtprnt.values.length; l++) {

        console.log("scnftprn: ", scnFtprnt.values[l]);

        var scnFtprntDates = scnFtprnt.values[l];

        if (scnFtprntDates.values.isArray) { console.log("is array")} else { console.log(scnFtprntDates.values.isArray)}

        console.log("array2: ", scnFtprntDates.values)

        for (var q=0; dtRng.length; q++) {

            for (var z=0; z<scnFtprntDates.values.length; z++) {

                if (dtRng[q] == scnFtprntDates.values[z].key) {

                } else {
                    console.log("pushing");
                    scnFtprntDates.values.push({key: dtRng[q], values: 0})

                }

            }
        }

    }
}

本质上,key.values.values中每个对象的键应该与数组dtRng中的一个值匹配。假设1483228800000数组中dtRng数组中的值key未在key.values.values对象数组的{key: 1483228800000, values: 0}中找到,它应该添加像var lngth = scnFtprntDates.values.length for (var q=0; q<dtRng.length; q++) { for (var z=0; z<lngth; z++) { if (dtRng[q] == parseInt(scnFtprntDates.values[z].key)) { break; } else { console.log("pushing") scnFtprntDates.values.push({key: dtRng[q], values: 0}) } } } 这样的对象那个阵列......

--- ---- EDIT

我尝试了它循环的部分并找到丢失的键并将它们推入数组中:

values: 0

但是他们得到了这个结果,它似乎仍然推进了数组中已有的值([ { "key": "Kallam 1204", "values": [ { "key": "Both", "values": [ { "key": "1420070400000", "values": 27927.7349421797 }, { "key": "1422748800000", "values": 27927.7349421797 }, { "key": "1425168000000", "values": 27927.7349421797 }, ... { "key": 1422748800000, "values": 0 }, { "key": 1425168000000, "values": 0 } ] } ] } ] )......

var costByScn = [{
  "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.734942179697
    }, {
      "key": "1441065600000",
      "values": 27927.7349421797
    }, {
      "key": "1443657600000",
      "values": 27927.7349421797
    }, {
      "key": "1446336000000",
      "values": 36929.2001421797
    }, {
      "key": "1448928000000",
      "values": 36929.2001421797
    }, {
      "key": "1451606400000",
      "values": 36929.2001421797
    }, {
      "key": "1454284800000",
      "values": 36929.200142179696
    }, {
      "key": "1456790400000",
      "values": 36929.2001421797
    }, {
      "key": "1459468800000",
      "values": 36929.200142179696
    }, {
      "key": "1462060800000",
      "values": 36929.200142179696
    }, {
      "key": "1464739200000",
      "values": 36929.200142179696
    }, {
      "key": "1467331200000",
      "values": 36929.2001421797
    }, {
      "key": "1470009600000",
      "values": 36929.2001421797
    }, {
      "key": "1472688000000",
      "values": 36929.2001421797
    }, {
      "key": "1475280000000",
      "values": 36929.2001421797
    }, {
      "key": "1477958400000",
      "values": 36929.200142179696
    }, {
      "key": "1480550400000",
      "values": 36929.200142179696
    }, {
      "key": "1483228800000",
      "values": 36929.200142179696
    }, {
      "key": "1485907200000",
      "values": 36929.200142179696
    }, {
      "key": "1488326400000",
      "values": 36929.200142179696
    }]
  }, {
    "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.8638
    }, {
      "key": "1493596800000",
      "values": 375564.8638
    }, {
      "key": "1496275200000",
      "values": 375564.8638
    }, {
      "key": "1498867200000",
      "values": 375564.86380000005
    }, {
      "key": "1501545600000",
      "values": 375564.86380000005
    }, {
      "key": "1504224000000",
      "values": 375564.86380000005
    }, {
      "key": "1506816000000",
      "values": 375564.8638
    }, {
      "key": "1509494400000",
      "values": 375564.86380000005
    }, {
      "key": "1512086400000",
      "values": 375564.86380000005
    }, {
      "key": "1514764800000",
      "values": 499244.18139999994
    }, {
      "key": "1517443200000",
      "values": 499244.1814
    }, {
      "key": "1519862400000",
      "values": 499244.1814
    }, {
      "key": "1522540800000",
      "values": 499244.18139999994
    }, {
      "key": "1525132800000",
      "values": 499244.1814
    }, {
      "key": "1527811200000",
      "values": 499244.1814
    }, {
      "key": "1530403200000",
      "values": 499244.1814
    }, {
      "key": "1533081600000",
      "values": 499244.18139999994
    }, {
      "key": "1535760000000",
      "values": 499244.1814
    }, {
      "key": "1538352000000",
      "values": 499244.1814
    }, {
      "key": "1541030400000",
      "values": 499244.1814
    }, {
      "key": "1543622400000",
      "values": 499244.1814
    }, {
      "key": "1546300800000",
      "values": 499244.1814
    }, {
      "key": "1548979200000",
      "values": 499244.1814
    }, {
      "key": "1551398400000",
      "values": 499244.1814
    }, {
      "key": "1554076800000",
      "values": 499244.1814
    }, {
      "key": "1556668800000",
      "values": 499244.1814
    }, {
      "key": "1559347200000",
      "values": 499244.1814
    }, {
      "key": "1561939200000",
      "values": 499244.18139999994
    }, {
      "key": "1564617600000",
      "values": 499244.1814
    }, {
      "key": "1567296000000",
      "values": 499244.18139999994
    }, {
      "key": "1569888000000",
      "values": 499244.18139999994
    }, {
      "key": "1572566400000",
      "values": 499244.1814
    }, {
      "key": "1575158400000",
      "values": 499244.18139999994
    }]
  }]
}]


var scn = costByScn

for (var i = 0; i < scn.length; i++) {

  var dtMax = 0;
  var dtMin = 1923273544000;


  var scnFtprnt = scn[i];

  console.log("scnftprnt: ", scnFtprnt);

  console.log("scnftprnt: ", scnFtprnt.values.length);

  for (j = 0; j < scnFtprnt.values.length; j++) {

    var scnFtprntDates = scnFtprnt.values[j];

    for (var q = 0; q < scnFtprntDates.values.length; q++) {

      var scnFtprntDt = scnFtprntDates.values[q].key

      if (dtMax < scnFtprntDt) {

        dtMax = scnFtprntDt
      }

      if (dtMin > scnFtprntDt) {

        dtMin = scnFtprntDt
      }


    }

  }

  var dtRng = []

  console.log("dtmin: ", dtMin);
  console.log("dtmax: ", dtMax);

  var offset = 5 * 60 * 60000

  dtMin = new Date(+dtMin + offset);
  dtMax = new Date(+dtMax + offset);

  console.log("dtmin: ", dtMin);
  console.log("dtmax: ", dtMax);

  while (dtMin <= dtMax) {

    dtRng.push(dtMin.getTime() - offset);

    dtMin = new Date(new Date(dtMin).setMonth(dtMin.getMonth() + 1));

  }

  console.log("dt rng:", dtRng);

  console.log("dt rng:", dtRng[0]);

                    for (l=0; l<scnFtprnt.values.length; l++) {
                        
                        console.log("scnftprn: ", scnFtprnt.values[l]);
                        
                        var scnFtprntDates = scnFtprnt.values[l];
                        var scn2 = scnFtprntDates.values
                        
                        if (scn2.isArray) { console.log("is array")} else { console.log("is not array")}
                        
                        console.log("array2: ", scnFtprntDates.values)
                        
                        var lngth = scnFtprntDates.values.length
                        
                        for (var q=0; q<dtRng.length; q++) {
                            console.log("part1: ", dtRng[q].toString());
                            if (_.some([scn2], ["key", dtRng[q]])) {
                                
                                
                                
                            } else {
                                
                                scn2.push({key: dtRng[q], values: 0})
                                
                            }
                            
                        }

                    }

}





console.log("data2: ", JSON.stringify(scn));

这里也是jsfiddle中代码和测试数据的副本:https://jsfiddle.net/Lhrwv09p/1/

&#13;
&#13;
a:first-child {  
    padding: 1em;   
    border: 0.2em solid #111;
}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

所以,我确实看到你在lodash上取得了一些进展,但是我发现lodash所做的很多事情都可以用普通的JavaScript来提供,如果你愿意更加冗长的话。下面我还添加了对最终结果的排序,以便更容易看到填充缺失值的位置。

我已将push更新为dtRng以将值作为字符串,因为我假设您从服务获取数据。这样可以更容易地完成下一部分。

scn2.some(function (item) { return item.key === dtRng[q]; })部分基本上等同于您更新的代码段中的lodash功能。

我将找到重复的结果更改为continue,以便它移动到数组中的下一个项目,而不是breakfor循环开始。

var sorter = function (a, b) {
  return a.key === b.key ? 0 : a.key > b.key ? 1 : -1;
}
var costByScn = [{
  "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.734942179697
    }, {
      "key": "1441065600000",
      "values": 27927.7349421797
    }, {
      "key": "1443657600000",
      "values": 27927.7349421797
    }, {
      "key": "1446336000000",
      "values": 36929.2001421797
    }, {
      "key": "1448928000000",
      "values": 36929.2001421797
    }, {
      "key": "1451606400000",
      "values": 36929.2001421797
    }, {
      "key": "1454284800000",
      "values": 36929.200142179696
    }, {
      "key": "1456790400000",
      "values": 36929.2001421797
    }, {
      "key": "1459468800000",
      "values": 36929.200142179696
    }, {
      "key": "1462060800000",
      "values": 36929.200142179696
    }, {
      "key": "1464739200000",
      "values": 36929.200142179696
    }, {
      "key": "1467331200000",
      "values": 36929.2001421797
    }, {
      "key": "1470009600000",
      "values": 36929.2001421797
    }, {
      "key": "1472688000000",
      "values": 36929.2001421797
    }, {
      "key": "1475280000000",
      "values": 36929.2001421797
    }, {
      "key": "1477958400000",
      "values": 36929.200142179696
    }, {
      "key": "1480550400000",
      "values": 36929.200142179696
    }, {
      "key": "1483228800000",
      "values": 36929.200142179696
    }, {
      "key": "1485907200000",
      "values": 36929.200142179696
    }, {
      "key": "1488326400000",
      "values": 36929.200142179696
    }]
  }, {
    "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.8638
    }, {
      "key": "1493596800000",
      "values": 375564.8638
    }, {
      "key": "1496275200000",
      "values": 375564.8638
    }, {
      "key": "1498867200000",
      "values": 375564.86380000005
    }, {
      "key": "1501545600000",
      "values": 375564.86380000005
    }, {
      "key": "1504224000000",
      "values": 375564.86380000005
    }, {
      "key": "1506816000000",
      "values": 375564.8638
    }, {
      "key": "1509494400000",
      "values": 375564.86380000005
    }, {
      "key": "1512086400000",
      "values": 375564.86380000005
    }, {
      "key": "1514764800000",
      "values": 499244.18139999994
    }, {
      "key": "1517443200000",
      "values": 499244.1814
    }, {
      "key": "1519862400000",
      "values": 499244.1814
    }, {
      "key": "1522540800000",
      "values": 499244.18139999994
    }, {
      "key": "1525132800000",
      "values": 499244.1814
    }, {
      "key": "1527811200000",
      "values": 499244.1814
    }, {
      "key": "1530403200000",
      "values": 499244.1814
    }, {
      "key": "1533081600000",
      "values": 499244.18139999994
    }, {
      "key": "1535760000000",
      "values": 499244.1814
    }, {
      "key": "1538352000000",
      "values": 499244.1814
    }, {
      "key": "1541030400000",
      "values": 499244.1814
    }, {
      "key": "1543622400000",
      "values": 499244.1814
    }, {
      "key": "1546300800000",
      "values": 499244.1814
    }, {
      "key": "1548979200000",
      "values": 499244.1814
    }, {
      "key": "1551398400000",
      "values": 499244.1814
    }, {
      "key": "1554076800000",
      "values": 499244.1814
    }, {
      "key": "1556668800000",
      "values": 499244.1814
    }, {
      "key": "1559347200000",
      "values": 499244.1814
    }, {
      "key": "1561939200000",
      "values": 499244.18139999994
    }, {
      "key": "1564617600000",
      "values": 499244.1814
    }, {
      "key": "1567296000000",
      "values": 499244.18139999994
    }, {
      "key": "1569888000000",
      "values": 499244.18139999994
    }, {
      "key": "1572566400000",
      "values": 499244.1814
    }, {
      "key": "1575158400000",
      "values": 499244.18139999994
    }]
  }]
}]


var scn = costByScn

for (var i = 0; i < scn.length; i++) {

  var dtMax = 0;
  var dtMin = 1923273544000;


  var scnFtprnt = scn[i];

  for (j = 0; j < scnFtprnt.values.length; j++) {

    var scnFtprntDates = scnFtprnt.values[j];

    for (var q = 0; q < scnFtprntDates.values.length; q++) {

      var scnFtprntDt = scnFtprntDates.values[q].key

      if (dtMax < scnFtprntDt) {

        dtMax = scnFtprntDt
      }

      if (dtMin > scnFtprntDt) {

        dtMin = scnFtprntDt
      }


    }

  }

  var dtRng = []

  var offset = 5 * 60 * 60000

  dtMin = new Date(+dtMin + offset);
  dtMax = new Date(+dtMax + offset);

  while (dtMin <= dtMax) {

    dtRng.push(String(dtMin.getTime() - offset));

    dtMin = new Date(new Date(dtMin).setMonth(dtMin.getMonth() + 1));

  }

  for (l = 0; l < scnFtprnt.values.length; l++) {

    var scnFtprntDates = scnFtprnt.values[l];
    var scn2 = scnFtprntDates.values

    var lngth = scnFtprntDates.values.length

    for (var q = 0; q < dtRng.length; q++) {
      if (scn2.some(function (item) { return item.key === dtRng[q]; })) { 
        continue; 
      } else {
        scn2.push({
          key: dtRng[q],
          values: 0
        })

      }

    }

    scn2.sort(sorter);
    
    scnFtprnt.values[l] = scn2;
  }

}

scn.sort(sorter);


console.log("data2: ", JSON.stringify(scn));