0:{entry_counts:2000,week_end_date:" 2016-09-01"} 1:{entry_counts:10000,week_end_date:" 2016-09-08"} 2:{ entry_counts:20000,week_end_date:" 2016-09-22"} 3:{entry_counts:40000,week_end_date:" 2016-09-29"} 4:{entry_counts:150000,week_end_date: " 2016年10月13日"}
答案 0 :(得分:1)
由于您的日期不包含任何类型的时区甚至时间数据,因此它将被视为UTC时区。我们真的不需要时刻帮助我们解决这个问题。
您还在代码中使用了很长的引用,这使得它更难以阅读并且更容易出错。 (例如:data.entryDetails[0].entry_counts[count].entry_counts;
)
我建议使用一些变量来减少它。这也可以更容易地检查值是否有效甚至定义。
使用时间和增加时间时,valueOf非常有用。我为你写了一个快速的脚本。我确信可以通过一些错误检查来确保它,并确保定义数据。除此之外它应该对你有用!
节日快乐!
var countData = [ {entry_counts: 2000, week_end_date: "2016-09-01"},{entry_counts: 10000, week_end_date: "2016-09-08"},{entry_counts: 20000, week_end_date: "2016-09-22"},{entry_counts: 40000, week_end_date: "2016-09-29"},{entry_counts: 150000, week_end_date: "2016-10-13"} ]
var today = ( new Date() ).valueOf()
var currentEntry = parseEntry( countData.shift() )
var nextEntry = parseEntry( countData.length ? countData.shift() : null )
var viewsArray = []
var datesArray = []
var time = currentEntry.dateValue
while( time <= today ){
// update currentEntry data used to the most up-to-date data we have
if( nextEntry && time > nextEntry.dateValue ) {
while( nextEntry && time > nextEntry.dateValue ){
currentEntry = nextEntry
nextEntry = parseEntry( countData.length ? countData.shift() : null )
}
}
viewsArray.push( currentEntry.views )
datesArray.push( new Date( time ) )
time += daysInMilliseconds(7)
}
console.log( viewsArray )
function parseEntry( entry ) {
if( !entry ) return null
entry.date = new Date( entry.week_end_date )
entry.dateValue = entry.date.valueOf()
entry.views = entry.entry_counts
return entry
}
function daysInMilliseconds( days ) {
return days * 24 * 60 * 60 * 1000
}