如果满足条件,则在数组中映射对象并显示日期

时间:2017-04-21 12:42:31

标签: javascript jquery mapping



var data = [
{
	from: "2017-04-21T12:00:00",
	to: "2017-04-22T18:00:00",
	period: "2"
},
{
	from: "2017-04-21T18:00:00",
	to: "2017-04-22T00:00:00",
	period: "3"
},
{
	from: "2017-04-22T00:00:00",
	to: "2017-04-22T06:00:00",
	period: "0"
},
{
	from: "2017-04-22T06:00:00",
	to: "2017-04-22T12:00:00",
	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>
&#13;
&#13;
&#13;

我从气象服务获取数据,每天将其分为四个时段:0, 1, 2, 4

句点为0的每个对象都是新一天的开始。为此,我想以日期格式from

显示日期字段DD. MM. YYYY HH:MM

对于期间不是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的第一个对象可以具有数据日&#34;今天&#34;。有人能指出我正确的方向吗?

<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 个答案:

答案 0 :(得分:0)

您可以使用jQuery:

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