用占位符填充数组Javascript [IE支持]

时间:2017-12-08 08:53:58

标签: javascript arrays internet-explorer

我有一个数组,我从谷歌分析的回复中获得。

我的数组看起来像这样:

var mobileYesterday = [
 {hour: "00", date: "20171208", pageviews: "1"},
 {hour: "06", date: "20171208", pageviews: "1"},
 {hour: "07", date: "20171208", pageviews: "5"},
 {hour: "08", date: "20171208", pageviews: "7"},
 {hour: "09", date: "20171208", pageviews: "4"}
];

由于24小时内存在一天,我希望能够用占位符填充数组,占位符将包含今天的日期,在上一个数组中未指定的小时数和pageviews = 0。数组也应该按小时排序,这样数组将如下所示:

 var mobileYesterday = [
     {hour: "00", date: "20171208", pageviews: "1"},
     {hour: "01", date: "20171208", pageviews: "0"},
     {hour: "02", date: "20171208", pageviews: "0"},
     {hour: "03", date: "20171208", pageviews: "0"},
     {hour: "04", date: "20171208", pageviews: "0"},
     {hour: "05", date: "20171208", pageviews: "0"},
     {hour: "06", date: "20171208", pageviews: "1"},
     {hour: "07", date: "20171208", pageviews: "5"},
     {hour: "08", date: "20171208", pageviews: "7"},
     {hour: "09", date: "20171208", pageviews: "4"},
     {hour: "10", date: "20171208", pageviews: "0"},
     {hour: "11", date: "20171208", pageviews: "0"},
     {hour: "12", date: "20171208", pageviews: "0"},
     {hour: "13", date: "20171208", pageviews: "0"}
     etc..
];

我试图用for循环遍历数组

var array = [];
    var date = moment().format("YYYYMMDD");

    var hourPlaceholder;
    for(var i = 0; i < 24; i++){
        if(i < 10){
            hourPlaceholder = '0'+i;
        }
        if(i > 9){
            hourPlaceholder = i;
        }


       for (var k = 0; k < mobileYesterday.length; k++){
           if(!mobileYesterday[k].uur == hourPlaceholder){
              var array_record = [
                  { "hour": hourPlaceholder, "date": date, "pageviews":0}
              ]
               array.push(array_record);
           }else{
               array.push(mobileYesterday[k]);
           }
       }
    }
    console.log(JSON.stringify(array));

但这只会返回有史以来最奇怪的数组:

[[{"hour":"00","date":"20171207","pageviews":0}],[{"hour":"00","date":"20171207","pageviews":0}],[{"hour":"00","date":"20171207","pageviews":0}],[{"hour":"00","date":"20171207","pageviews":0}],[{"hour":"00","date":"20171207","pageviews":0}],[{"hour":"00","date":"20171207","pageviews":0}],[{"hour":"00","date":"20171207","pageviews":0}],[{"hour":"00","date":"20171207","pageviews":0}],[{"hour":"00","date":"20171207","pageviews":0}],[{"hour":"00","date":"20171207","pageviews":0}],[{"hour":"00","date":"20171207","pageviews":0}],[{"hour":"00","date":"20171207","pageviews":0}],[{"hour":"00","date":"20171207","pageviews":0}],[{"hour":"00","date":"20171207","pageviews":0}],[{"hour":"00","date":"20171207","pageviews":0}],[{"hour":"00","date":"20171207","pageviews":0}],[{"hour":"00","date":"20171207","pageviews":0}],[{"hour":"00","date":"20171207","pageviews":0}],{"hour":"01","date":"20171207","pageviews":"1"},{"hour":"07","date":"20171207","pageviews":"5"},{"hour":"08","date":"20171207","pageviews":"7"},{"hour":"09","date":"20171207","pageviews":"8"},{"hour":"10","date":"20171207","pageviews":"8"},{"hour":"11","date":"20171207","pageviews":"9"},{"hour":"12","date":"20171207","pageviews":"9"},{"hour":"13","date":"20171207","pageviews":"6"},{"hour":"14","date":"20171207","pageviews":"5"},{"hour":"15","date":"20171207","pageviews":"6"},{"hour":"16","date":"20171207","pageviews":"7"},{"hour":"17","date":"20171207","pageviews":"7"},{"hour":"18","date":"20171207","pageviews":"4"},{"hour":"19","date":"20171207","pageviews":"7"},{"hour":"20","date":"20171207","pageviews":"5"},{"hour":"21","date":"20171207","pageviews":"8"},{"hour":"22","date":"20171207","pageviews":"5"},{"hour":"23","date":"20171207","pageviews":"3"},{"hour":"01","date":"20171207","pageviews":"1"},{"hour":"07","date":"20171207","pageviews":"5"},{"hour":"08","date":"20171207","pageviews":"7"},{"hour":"09","date":"20171207","pageviews":"8"},{"hour":"10","date":"20171207","pageviews":"8"},{"hour":"11","date":"20171207","pageviews":"9"},{"hour":"12","date":"20171207","pageviews":"9"},{"hour":"13","date":"20171207","pageviews":"6"},{"hour":"14","date":"20171207","pageviews":"5"},{"hour":"15","date":"20171207","pageviews":"6"},{"hour":"16","date":"20171207","pageviews":"7"},{"hour":"17","date":"20171207","pageviews":"7"},{"hour":"18","date":"20171207","pageviews":"4"},{"hour":"19","date":"20171207","pageviews":"7"},{"hour":"20","date":"20171207","pageviews":"5"},{"hour":"21","date":"20171207","pageviews":"8"},{"hour":"22","date":"20171207","pageviews":"5"},{"hour":"23","date":"20171207","pageviews":"3"},{"hour":"01","date":"20171207","pageviews":"1"},{"hour":"07","date":"20171207","pageviews":"5"},{"hour":"08","date":"20171207","pageviews":"7"},{"hour":"09","date":"20171207","pageviews":"8"},{"hour":"10","date":"20171207","pageviews":"8"},{"hour":"11","date":"20171207","pageviews":"9"},{"hour":"12","date":"20171207","pageviews":"9"},{"hour":"13","date":"20171207","pageviews":"6"},{"hour":"14","date":"20171207","pageviews":"5"},{"hour":"15","date":"20171207","pageviews":"6"},{"hour":"16","date":"20171207","pageviews":"7"},{"hour":"17","date":"20171207","pageviews":"7"},{"hour":"18","date":"20171207","pageviews":"4"},{"hour":"19","date":"20171207","pageviews":"7"},{"hour":"20","date":"20171207","pageviews":"5"},{"hour":"21","date":"20171207","pageviews":"8"},{"hour":"22","date":"20171207","pageviews":"5"},{"hour":"23","date":"20171207","pageviews":"3"},{"hour":"01","date":"20171207","pageviews":"1"},{"hour":"07","date":"20171207","pageviews":"5"},{"hour":"08","date":"20171207","pageviews":"7"},{"hour":"09","date":"20171207","pageviews":"8"},{"hour":"10","date":"20171207","pageviews":"8"},{"hour":"11","date":"20171207","pageviews":"9"},{"hour":"12","date":"20171207","pageviews":"9"},{"hour":"13","date":"20171207","pageviews":"6"},{"hour":"14","date":"20171207","pageviews":"5"},{"hour":"15","date":"20171207","pageviews":"6"},{"hour":"16","date":"20171207","pageviews":"7"},{"hour":"17","date":"20171207","pageviews":"7"},{"hour":"18","date":"20171207","pageviews":"4"},{"hour":"19","date":"20171207","pageviews":"7"},{"hour":"20","date":"20171207","pageviews":"5"},{"hour":"21","date":"20171207","pageviews":"8"},{"hour":"22","date":"20171207","pageviews":"5"},{"hour":"23","date":"20171207","pageviews":"3"},{"hour":"01","date":"20171207","pageviews":"1"},{"hour":"07","date":"20171207","pageviews":"5"},{"hour":"08","date":"20171207","pageviews":"7"},{"hour":"09","date":"20171207","pageviews":"8"},{"hour":"10","date":"20171207","pageviews":"8"},{"hour":"11","date":"20171207","pageviews":"9"},{"hour":"12","date":"20171207","pageviews":"9"},{"hour":"13","date":"20171207","pageviews":"6"},{"hour":"14","date":"20171207","pageviews":"5"},{"hour":"15","date":"20171207","pageviews":"6"},{"hour":"16","date":"20171207","pageviews":"7"},{"hour":"17","date":"20171207","pageviews":"7"},{"hour":"18","date":"20171207","pageviews":"4"},{"hour":"19","date":"20171207","pageviews":"7"},{"hour":"20","date":"20171207","pageviews":"5"},{"hour":"21","date":"20171207","pageviews":"8"},{"hour":"22","date":"20171207","pageviews":"5"},{"hour":"23","date":"20171207","pageviews":"3"},{"hour":"01","date":"20171207","pageviews":"1"},{"hour":"07","date":"20171207","pageviews":"5"},{"hour":"08","date":"20171207","pageviews":"7"},{"hour":"09","date":"20171207","pageviews":"8"},{"hour":"10","date":"20171207","pageviews":"8"},{"hour":"11","date":"20171207","pageviews":"9"},{"hour":"12","date":"20171207","pageviews":"9"},{"hour":"13","date":"20171207","pageviews":"6"},{"hour":"14","date":"20171207","pageviews":"5"},{"hour":"15","date":"20171207","pageviews":"6"},{"hour":"16","date":"20171207","pageviews":"7"},{"hour":"17","date":"20171207","pageviews":"7"},{"hour":"18","date":"20171207","pageviews":"4"},{"hour":"19","date":"20171207","pageviews":"7"},{"hour":"20","date":"20171207","pageviews":"5"},{"hour":"21","date":"20171207","pageviews":"8"},{"hour":"22","date":"20171207","pageviews":"5"},{"hour":"23","date":"20171207","pageviews":"3"},{"hour":"01","date":"20171207","pageviews":"1"},{"hour":"07","date":"20171207","pageviews":"5"},{"hour":"08","date":"20171207","pageviews":"7"},{"hour":"09","date":"20171207","pageviews":"8"},{"hour":"10","date":"20171207","pageviews":"8"},{"hour":"11","date":"20171207","pageviews":"9"},{"hour":"12","date":"20171207","pageviews":"9"},{"hour":"13","date":"20171207","pageviews":"6"},{"hour":"14","date":"20171207","pageviews":"5"},{"hour":"15","date":"20171207","pageviews":"6"},{"hour":"16","date":"20171207","pageviews":"7"},{"hour":"17","date":"20171207","pageviews":"7"},{"hour":"18","date":"20171207","pageviews":"4"},{"hour":"19","date":"20171207","pageviews":"7"},{"hour":"20","date":"20171207","pageviews":"5"},{"hour":"21","date":"20171207","pageviews":"8"},{"hour":"22","date":"20171207","pageviews":"5"},{"hour":"23","date":"20171207","pageviews":"3"},{"hour":"01","date":"20171207","pageviews":"1"},{"hour":"07","date":"20171207","pageviews":"5"},{"hour":"08","date":"20171207","pageviews":"7"},{"hour":"09","date":"20171207","pageviews":"8"},{"hour":"10","date":"20171207","pageviews":"8"},{"hour":"11","date":"20171207","pageviews":"9"},{"hour":"12","date":"20171207","pageviews":"9"},{"hour":"13","date":"20171207","pageviews":"6"},{"hour":"14","date":"20171207","pageviews":"5"},{"hour":"15","date":"20171207","pageviews":"6"},{"hour":"16","date":"20171207","pageviews":"7"},{"hour":"17","date":"20171207","pageviews":"7"},{"hour":"18","date":"20171207","pageviews":"4"},{"hour":"19","date":"20171207","pageviews":"7"},{"hour":"20","date":"20171207","pageviews":"5"},{"hour":"21","date":"20171207","pageviews":"8"},{"hour":"22","date":"20171207","pageviews":"5"},{"hour":"23","date":"20171207","pageviews":"3"},{"hour":"01","date":"20171207","pageviews":"1"},{"hour":"07","date":"20171207","pageviews":"5"},{"hour":"08","date":"20171207","pageviews":"7"},{"hour":"09","date":"20171207","pageviews":"8"},{"hour":"10","date":"20171207","pageviews":"8"},{"hour":"11","date":"20171207","pageviews":"9"},{"hour":"12","date":"20171207","pageviews":"9"},{"hour":"13","date":"20171207","pageviews":"6"},{"hour":"14","date":"20171207","pageviews":"5"},{"hour":"15","date":"20171207","pageviews":"6"},{"hour":"16","date":"20171207","pageviews":"7"},{"hour":"17","date":"20171207","pageviews":"7"},{"hour":"18","date":"20171207","pageviews":"4"},{"hour":"19","date":"20171207","pageviews":"7"},{"hour":"20","date":"20171207","pageviews":"5"},{"hour":"21","date":"20171207","pageviews":"8"},{"hour":"22","date":"20171207","pageviews":"5"},{"hour":"23","date":"20171207","pageviews":"3"},{"hour":"01","date":"20171207","pageviews":"1"},{"hour":"07","date":"20171207","pageviews":"5"},{"hour":"08","date":"20171207","pageviews":"7"},{"hour":"09","date":"20171207","pageviews":"8"},{"hour":"10","date":"20171207","pageviews":"8"},{"hour":"11","date":"20171207","pageviews":"9"},{"hour":"12","date":"20171207","pageviews":"9"},{"hour":"13","date":"20171207","pageviews":"6"},{"hour":"14","date":"20171207","pageviews":"5"},{"hour":"15","date":"20171207","pageviews":"6"},{"hour":"16","date":"20171207","pageviews":"7"},{"hour":"17","date":"20171207","pageviews":"7"},{"hour":"18","date":"20171207","pageviews":"4"},{"hour":"19","date":"20171207","pageviews":"7"},{"hour":"20","date":"20171207","pageviews":"5"},{"hour":"21","date":"20171207","pageviews":"8"},{"hour":"22","date":"20171207","pageviews":"5"},{"hour":"23","date":"20171207","pageviews":"3"},{"hour":"01","date":"20171207","pageviews":"1"},{"hour":"07","date":"20171207","pageviews":"5"},{"hour":"08","date":"20171207","pageviews":"7"},{"hour":"09","date":"20171207","pageviews":"8"},{"hour":"10","date":"20171207","pageviews":"8"},{"hour":"11","date":"20171207","pageviews":"9"},{"hour":"12","date":"20171207","pageviews":"9"},{"hour":"13","date":"20171207","pageviews":"6"},{"hour":"14","date":"20171207","pageviews":"5"},{"hour":"15","date":"20171207","pageviews":"6"},{"hour":"16","date":"20171207","pageviews":"7"},{"hour":"17","date":"20171207","pageviews":"7"},{"hour":"18","date":"20171207","pageviews":"4"},{"hour":"19","date":"20171207","pageviews":"7"},{"hour":"20","date":"20171207","pageviews":"5"},{"hour":"21","date":"20171207","pageviews":"8"},{"hour":"22","date":"20171207","pageviews":"5"},{"hour":"23","date":"20171207","pageviews":"3"},{"hour":"01","date":"20171207","pageviews":"1"},{"hour":"07","date":"20171207","pageviews":"5"},{"hour":"08","date":"20171207","pageviews":"7"},{"hour":"09","date":"20171207","pageviews":"8"},{"hour":"10","date":"20171207","pageviews":"8"},{"hour":"11","date":"20171207","pageviews":"9"},{"hour":"12","date":"20171207","pageviews":"9"},{"hour":"13","date":"20171207","pageviews":"6"},{"hour":"14","date":"20171207","pageviews":"5"},{"hour":"15","date":"20171207","pageviews":"6"},{"hour":"16","date":"20171207","pageviews":"7"},{"hour":"17","date":"20171207","pageviews":"7"},{"hour":"18","date":"20171207","pageviews":"4"},{"hour":"19","date":"20171207","pageviews":"7"},{"hour":"20","date":"20171207","pageviews":"5"},{"hour":"21","date":"20171207","pageviews":"8"},{"hour":"22","date":"20171207","pageviews":"5"},{"hour":"23","date":"20171207","pageviews":"3"},{"hour":"01","date":"20171207","pageviews":"1"},{"hour":"07","date":"20171207","pageviews":"5"},{"hour":"08","date":"20171207","pageviews":"7"},{"hour":"09","date":"20171207","pageviews":"8"},{"hour":"10","date":"20171207","pageviews":"8"},{"hour":"11","date":"20171207","pageviews":"9"},{"hour":"12","date":"20171207","pageviews":"9"},{"hour":"13","date":"20171207","pageviews":"6"},{"hour":"14","date":"20171207","pageviews":"5"},{"hour":"15","date":"20171207","pageviews":"6"},{"hour":"16","date":"20171207","pageviews":"7"},{"hour":"17","date":"20171207","pageviews":"7"},{"hour":"18","date":"20171207","pageviews":"4"},{"hour":"19","date":"20171207","pageviews":"7"},{"hour":"20","date":"20171207","pageviews":"5"},{"hour":"21","date":"20171207","pageviews":"8"},{"hour":"22","date":"20171207","pageviews":"5"},{"hour":"23","date":"20171207","pageviews":"3"},{"hour":"01","date":"20171207","pageviews":"1"},{"hour":"07","date":"20171207","pageviews":"5"},{"hour":"08","date":"20171207","pageviews":"7"},{"hour":"09","date":"20171207","pageviews":"8"},{"hour":"10","date":"20171207","pageviews":"8"},{"hour":"11","date":"20171207","pageviews":"9"},{"hour":"12","date":"20171207","pageviews":"9"},{"hour":"13","date":"20171207","pageviews":"6"},{"hour":"14","date":"20171207","pageviews":"5"},{"hour":"15","date":"20171207","pageviews":"6"},{"hour":"16","date":"20171207","pageviews":"7"},{"hour":"17","date":"20171207","pageviews":"7"},{"hour":"18","date":"20171207","pageviews":"4"},{"hour":"19","date":"20171207","pageviews":"7"},{"hour":"20","date":"20171207","pageviews":"5"},{"hour":"21","date":"20171207","pageviews":"8"},{"hour":"22","date":"20171207","pageviews":"5"},{"hour":"23","date":"20171207","pageviews":"3"},{"hour":"01","date":"20171207","pageviews":"1"},{"hour":"07","date":"20171207","pageviews":"5"},{"hour":"08","date":"20171207","pageviews":"7"},{"hour":"09","date":"20171207","pageviews":"8"},{"hour":"10","date":"20171207","pageviews":"8"},{"hour":"11","date":"20171207","pageviews":"9"},{"hour":"12","date":"20171207","pageviews":"9"},{"hour":"13","date":"20171207","pageviews":"6"},{"hour":"14","date":"20171207","pageviews":"5"},{"hour":"15","date":"20171207","pageviews":"6"},{"hour":"16","date":"20171207","pageviews":"7"},{"hour":"17","date":"20171207","pageviews":"7"},{"hour":"18","date":"20171207","pageviews":"4"},{"hour":"19","date":"20171207","pageviews":"7"},{"hour":"20","date":"20171207","pageviews":"5"},{"hour":"21","date":"20171207","pageviews":"8"},{"hour":"22","date":"20171207","pageviews":"5"},{"hour":"23","date":"20171207","pageviews":"3"},{"hour":"01","date":"20171207","pageviews":"1"},{"hour":"07","date":"20171207","pageviews":"5"},{"hour":"08","date":"20171207","pageviews":"7"},{"hour":"09","date":"20171207","pageviews":"8"},{"hour":"10","date":"20171207","pageviews":"8"},{"hour":"11","date":"20171207","pageviews":"9"},{"hour":"12","date":"20171207","pageviews":"9"},{"hour":"13","date":"20171207","pageviews":"6"},{"hour":"14","date":"20171207","pageviews":"5"},{"hour":"15","date":"20171207","pageviews":"6"},{"hour":"16","date":"20171207","pageviews":"7"},{"hour":"17","date":"20171207","pageviews":"7"},{"hour":"18","date":"20171207","pageviews":"4"},{"hour":"19","date":"20171207","pageviews":"7"},{"hour":"20","date":"20171207","pageviews":"5"},{"hour":"21","date":"20171207","pageviews":"8"},{"hour":"22","date":"20171207","pageviews":"5"},{"hour":"23","date":"20171207","pageviews":"3"},{"hour":"01","date":"20171207","pageviews":"1"},{"hour":"07","date":"20171207","pageviews":"5"},{"hour":"08","date":"20171207","pageviews":"7"},{"hour":"09","date":"20171207","pageviews":"8"},{"hour":"10","date":"20171207","pageviews":"8"},{"hour":"11","date":"20171207","pageviews":"9"},{"hour":"12","date":"20171207","pageviews":"9"},{"hour":"13","date":"20171207","pageviews":"6"},{"hour":"14","date":"20171207","pageviews":"5"},{"hour":"15","date":"20171207","pageviews":"6"},{"hour":"16","date":"20171207","pageviews":"7"},{"hour":"17","date":"20171207","pageviews":"7"},{"hour":"18","date":"20171207","pageviews":"4"},{"hour":"19","date":"20171207","pageviews":"7"},{"hour":"20","date":"20171207","pageviews":"5"},{"hour":"21","date":"20171207","pageviews":"8"},{"hour":"22","date":"20171207","pageviews":"5"},{"hour":"23","date":"20171207","pageviews":"3"},{"hour":"01","date":"20171207","pageviews":"1"},{"hour":"07","date":"20171207","pageviews":"5"},{"hour":"08","date":"20171207","pageviews":"7"},{"hour":"09","date":"20171207","pageviews":"8"},{"hour":"10","date":"20171207","pageviews":"8"},{"hour":"11","date":"20171207","pageviews":"9"},{"hour":"12","date":"20171207","pageviews":"9"},{"hour":"13","date":"20171207","pageviews":"6"},{"hour":"14","date":"20171207","pageviews":"5"},{"hour":"15","date":"20171207","pageviews":"6"},{"hour":"16","date":"20171207","pageviews":"7"},{"hour":"17","date":"20171207","pageviews":"7"},{"hour":"18","date":"20171207","pageviews":"4"},{"hour":"19","date":"20171207","pageviews":"7"},{"hour":"20","date":"20171207","pageviews":"5"},{"hour":"21","date":"20171207","pageviews":"8"},{"hour":"22","date":"20171207","pageviews":"5"},{"hour":"23","date":"20171207","pageviews":"3"},{"hour":"01","date":"20171207","pageviews":"1"},{"hour":"07","date":"20171207","pageviews":"5"},{"hour":"08","date":"20171207","pageviews":"7"},{"hour":"09","date":"20171207","pageviews":"8"},{"hour":"10","date":"20171207","pageviews":"8"},{"hour":"11","date":"20171207","pageviews":"9"},{"hour":"12","date":"20171207","pageviews":"9"},{"hour":"13","date":"20171207","pageviews":"6"},{"hour":"14","date":"20171207","pageviews":"5"},{"hour":"15","date":"20171207","pageviews":"6"},{"hour":"16","date":"20171207","pageviews":"7"},{"hour":"17","date":"20171207","pageviews":"7"},{"hour":"18","date":"20171207","pageviews":"4"},{"hour":"19","date":"20171207","pageviews":"7"},{"hour":"20","date":"20171207","pageviews":"5"},{"hour":"21","date":"20171207","pageviews":"8"},{"hour":"22","date":"20171207","pageviews":"5"},{"hour":"23","date":"20171207","pageviews":"3"},{"hour":"01","date":"20171207","pageviews":"1"},{"hour":"07","date":"20171207","pageviews":"5"},{"hour":"08","date":"20171207","pageviews":"7"},{"hour":"09","date":"20171207","pageviews":"8"},{"hour":"10","date":"20171207","pageviews":"8"},{"hour":"11","date":"20171207","pageviews":"9"},{"hour":"12","date":"20171207","pageviews":"9"},{"hour":"13","date":"20171207","pageviews":"6"},{"hour":"14","date":"20171207","pageviews":"5"},{"hour":"15","date":"20171207","pageviews":"6"},{"hour":"16","date":"20171207","pageviews":"7"},{"hour":"17","date":"20171207","pageviews":"7"},{"hour":"18","date":"20171207","pageviews":"4"},{"hour":"19","date":"20171207","pageviews":"7"},{"hour":"20","date":"20171207","pageviews":"5"},{"hour":"21","date":"20171207","pageviews":"8"},{"hour":"22","date":"20171207","pageviews":"5"},{"hour":"23","date":"20171207","pageviews":"3"},{"hour":"01","date":"20171207","pageviews":"1"},{"hour":"07","date":"20171207","pageviews":"5"},{"hour":"08","date":"20171207","pageviews":"7"},{"hour":"09","date":"20171207","pageviews":"8"},{"hour":"10","date":"20171207","pageviews":"8"},{"hour":"11","date":"20171207","pageviews":"9"},{"hour":"12","date":"20171207","pageviews":"9"},{"hour":"13","date":"20171207","pageviews":"6"},{"hour":"14","date":"20171207","pageviews":"5"},{"hour":"15","date":"20171207","pageviews":"6"},{"hour":"16","date":"20171207","pageviews":"7"},{"hour":"17","date":"20171207","pageviews":"7"},{"hour":"18","date":"20171207","pageviews":"4"},{"hour":"19","date":"20171207","pageviews":"7"},{"hour":"20","date":"20171207","pageviews":"5"},{"hour":"21","date":"20171207","pageviews":"8"},{"hour":"22","date":"20171207","pageviews":"5"},{"hour":"23","date":"20171207","pageviews":"3"},{"hour":"01","date":"20171207","pageviews":"1"},{"hour":"07","date":"20171207","pageviews":"5"},{"hour":"08","date":"20171207","pageviews":"7"},{"hour":"09","date":"20171207","pageviews":"8"},{"hour":"10","date":"20171207","pageviews":"8"},{"hour":"11","date":"20171207","pageviews":"9"},{"hour":"12","date":"20171207","pageviews":"9"},{"hour":"13","date":"20171207","pageviews":"6"},{"hour":"14","date":"20171207","pageviews":"5"},{"hour":"15","date":"20171207","pageviews":"6"},{"hour":"16","date":"20171207","pageviews":"7"},{"hour":"17","date":"20171207","pageviews":"7"},{"hour":"18","date":"20171207","pageviews":"4"},{"hour":"19","date":"20171207","pageviews":"7"},{"hour":"20","date":"20171207","pageviews":"5"},{"hour":"21","date":"20171207","pageviews":"8"},{"hour":"22","date":"20171207","pageviews":"5"},{"hour":"23","date":"20171207","pageviews":"3"},{"hour":"01","date":"20171207","pageviews":"1"},{"hour":"07","date":"20171207","pageviews":"5"},{"hour":"08","date":"20171207","pageviews":"7"},{"hour":"09","date":"20171207","pageviews":"8"},{"hour":"10","date":"20171207","pageviews":"8"},{"hour":"11","date":"20171207","pageviews":"9"},{"hour":"12","date":"20171207","pageviews":"9"},{"hour":"13","date":"20171207","pageviews":"6"},{"hour":"14","date":"20171207","pageviews":"5"},{"hour":"15","date":"20171207","pageviews":"6"},{"hour":"16","date":"20171207","pageviews":"7"},{"hour":"17","date":"20171207","pageviews":"7"},{"hour":"18","date":"20171207","pageviews":"4"},{"hour":"19","date":"20171207","pageviews":"7"},{"hour":"20","date":"20171207","pageviews":"5"},{"hour":"21","date":"20171207","pageviews":"8"},{"hour":"22","date":"20171207","pageviews":"5"},{"hour":"23","date":"20171207","pageviews":"3"}]

2 个答案:

答案 0 :(得分:1)

考虑到数组的结构,mobileY yesterday.indexOf(10)将始终返回 -1

此外,mobileYesterday[k].uur始终为undefined,因为您的对象中没有名为uur的属性。

使用Array 构造函数以及map

var pad2 = ( int ) => ( "0" + int ).slice( -2 ); 
var output = Array(24).join("0").split("").map( function(it, in){
  in = pad2(in);
  var doesExists = mobileYesterday.find( s => s.hour == in ); //find existing object
  if( !!doesExists )
  {
     return doesExists;
  } 
  else
  {
     return { hour : in, date : "20171208", pageviews : 0 }; //if object doesn't exists return a new one
  }
});

<强>演示

&#13;
&#13;
var mobileYesterday = [
 {hour: "00", date: "20171208", pageviews: "1"},
 {hour: "06", date: "20171208", pageviews: "1"},
 {hour: "07", date: "20171208", pageviews: "5"},
 {hour: "08", date: "20171208", pageviews: "7"},
 {hour: "09", date: "20171208", pageviews: "4"}
];

var pad2 = (int) => ("0" + int).slice(-2);
var output = Array(24).join("0").split("").map(function(it, ind ) { 
  ind = pad2( ind );
  var doesExists = mobileYesterday.find(s => s.hour == ind );
  if (!!doesExists) {
    return doesExists;
  } else {
    return {
      hour: ind ,
      date: "20171208",
      pageviews: 0
    }; //if object doesn't exists return a new one
  }
});
console.log( output );
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您可以对格式化的hour采用简化方法并省略moment,因为您可以从给定数组中获取日期。

获取给定数组的索引,然后使用hour进行检查。

&#13;
&#13;
function fillArray(data) {
    var array = [],
        hour,
        i, j = 0;

    for (i = 0; i < 24; i++) {
        hour = ('0' + i).slice(-2);
        array.push(data[j] && data[j].hour === hour
            ? data[j++]
            : { hour: hour, date: data[0].date, pageviews: 0 }
        );
    }
    return array;
}

var mobileYesterday = [{ hour: "00", date: "20171208", pageviews: "1" }, { hour: "06", date: "20171208", pageviews: "1" }, { hour: "07", date: "20171208", pageviews: "5" }, { hour: "08", date: "20171208", pageviews: "7" }, { hour: "09", date: "20171208", pageviews: "4" }];

console.log(fillArray(mobileYesterday));
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;