2016-12-12 5 views
0

のは、私はこのような構造を持つオブジェクトがあるとしましょう配列に何千ものオブジェクトを押して終わる:ループが意図せず

私はminとmax日時値を見つけるために行うことですしようとしている何
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で、私の最大の15751.584億(月ごとの増分)であれば、それは今values 0の

で欠落しているペアを作成する必要があり、私はの束でこれを基本的に持ってきましたこれをやろうとするためのループ(これを行うより効率的なやり方があれば私は興味があります)。私は今、何をしようとしている

作成およびarraryタイムスタンプのdtRngdtMindtMaxの間、月によって増分ミリ秒で、その後、それらの増分のそれぞれは、配列内のオブジェクトを持っていることを確認しています。

私は近いと思いますが、今はこの部分で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}) 

       } 

      } 
     } 

    } 
} 

、各オブジェクトのキーin key.values.valuesは配列dtRngのいずれかの値と一致する必要があります。

はEDIT --- ... dtRng配列の値1483228800000は、オブジェクトのkey.values.values配列のkeyに見つかりませんでした、それはその配列に{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}) 

     } 

    } 
} 

を、まだ(値に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 
      } 
     ] 
     } 
    ] 
    } 
] 

は同様にjsfiddleのコードやテストデータのコピーです:https://jsfiddle.net/Lhrwv09p/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]; 
 

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

+0

_は「今、私は基本的にこれを実行しようとするループの束でこれを持ってきましたが、同様にこれを行うには、より効率的な方法はありますか?」_あなた可能性[Lodash](https://lodash.com) –

+1

を使用してください。ここでは配列とオブジェクトをネストしていますが、結果がどのように見えるかははっきりしていません。前と後の例を明確に教えてください。 – JLRishe

+0

@JLRishe最後に編集して、それを説明しようとしました... – user2061886

答えて

0

だから、私はあなたがlodashでいくつかの進歩をしていたことがわかりましたが、私は、あなたがもう少し冗長になりたいなら、多くのロダッシュのことが普通のJavaScriptで利用可能であることを発見しました。以下では、最後の結果に並べ替えを追加して、不足している値がどこに書き込まれているかを簡単に確認できるようにしました。pushdtRngに更新しました。あなたがサービスからデータを取得しているということです。これにより、次の部分を簡単に実行できます。

scn2.some(function (item) { return item.key === dtRng[q]; })の部分は、基本的には更新されたスニペットでのlodash関数と同じです。

continueの複製結果がforループの外側にあるbreakではなく配列の次の項目に移動するように変更しました。

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

関連する問題