2017-04-21 173 views
-1

Map對象,如果條件滿足

var data = [ 
 
{ 
 
\t from: "2017-04-21T12:00:00", 
 
\t to: "2017-04-22T18:00:00", 
 
\t period: "2" 
 
}, 
 
{ 
 
\t from: "2017-04-21T18:00:00", 
 
\t to: "2017-04-22T00:00:00", 
 
\t period: "3" 
 
}, 
 
{ 
 
\t from: "2017-04-22T00:00:00", 
 
\t to: "2017-04-22T06:00:00", 
 
\t period: "0" 
 
}, 
 
{ 
 
\t from: "2017-04-22T06:00:00", 
 
\t to: "2017-04-22T12:00:00", 
 
\t period: "1" 
 
}];
<div data-day="today"> 
 
    <p>Today</p> 
 
    <li>12:00 - 18:00: rain</li> 
 
    <li>18:00 - 00:00: clouds</li> 
 
</div> 
 

 
<div data-day="2017-04-22"> 
 
    <p>22. april 2017</p> 
 
    <li>00:00 - 06:00: sun</li> 
 
    <li>06:00 - 12:00: snow</li> 
 
</div>

我從氣象服務,其中每天被分爲四個時期中獲取數據:0, 1, 2, 4

週期爲0的每個對象都是新的一天的開始。爲此,我想顯示的日期格式DD. MM. YYYY HH:MM

對於對象的日期字段from,其中一段是不是0,我只需要顯示的時間格式fromHH:MM。我正在使用Momentjs來解析日期,但需要關於如何分組每天的輸入。

映射的對象的路要走,或有沒有人有如何實現這個好建議?

第一個對象將始終爲當前期間,因此這可能會有所不同。

{ 
    from: "2017-04-21T12:00:00", 
    to: "2017-04-22T18:00:00", 
    period: "2", 
    forecast: "rain" 
}, 
{ 
    from: "2017-04-21T18:00:00", 
    to: "2017-04-22T00:00:00", 
    period: "3", 
    forecast: "clouds" 
}, 
{ 
    from: "2017-04-22T00:00:00", 
    to: "2017-04-22T06:00:00", 
    period: "0", 
    forecast: "sun" 
}, 
{ 
    from: "2017-04-22T06:00:00", 
    to: "2017-04-22T12:00:00", 
    period: "1", 
    forecast: "snow" 
}, 
etc... 

因此,爲此我想爲每個新的一天輸出一個新的div。直到第一個週期= 0的第一個對象可能有「今天」的數據日。有人能指引我朝着正確的方向嗎?

<div data-day="today"> 
    <p>Today</p> 
    <li>12:00 - 18:00: rain</li> 
    <li>18:00 - 00:00: clouds</li> 
</div> 

<div data-day="2017-04-22"> 
    <p>22. april 2017</p> 
    <li>00:00 - 06:00: sun</li> 
    <li>06:00 - 12:00: snow</li> 
</div> 
+1

點擊'<>'來顯示[MCVE]和預期輸出 – mplungjan

+0

恐怕目前還不清楚你問這裏。 –

+0

請向我們展示您的預期產出。 –

回答

0

您可以使用jQuery它:

var firstitem = $.grep(allitems, item => (item.period ==0))[0]