gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 20:55:27 25 4
gpt4 key购买 nike

我有一个从 google Analytics 的响应中获得的数组。

我的数组看起来像这样:

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"}]

最佳答案

鉴于您的数组结构,mobileYesterday.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
}
});

演示

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

关于javascript - 用占位符填充数组 Javascript [IE 支持],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47710710/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com