简体   繁体   English

根据json对象中的日期和时间值过滤json输出

[英]Filter json output based on date & time value inside json object

I'm building a play data reporting tool for a radio station. 我正在为广播电台构建播放数据报告工具。 The idea is to fetch play history from the playout software's API, then manually add tracks played from outside the playout software. 这个想法是从播放软件的API获取播放历史记录,然后手动添加从播放软件外部播放的曲目。 The API outputs a daily playlog, from 00:00 to 24:00. API每天从00:00到24:00输出播放记录。

When in action, the user logs in to the system, sets the date and time of his/her show, then fetches the songs played in that timeframe into the report. 在实际操作中,用户登录到系统,设置其演出的日期和时间,然后将在该时间范围内播放的歌曲提取到报告中。 What I don't know is how to filter the results from the json based on the hour in the "date" -value. 我不知道如何根据“ date” -value中的小时从json过滤结果。 HTML, jQuery, JSON examples below. 以下是HTML,jQuery,JSON示例。

I've searched for other questions with similar problems but haven't find an answer. 我搜索了类似问题的其他问题,但没有找到答案。 I'm pretty new to this so I don't know where to look next. 我对此很陌生,所以我不知道下一步要去哪里。 Any help or tips how to improve my code is greatly appreciated! 非常感谢任何帮助或提示,以改进我的代码!

jquery:

$('.gettracks').click(function() {
  const date = document.getElementById('date').value;
  const starthh = document.getElementById('starthh').value;
  const startmm = document.getElementById('startmm').value;
  const endhh = document.getElementById('endhh').value;
  const endmm = document.getElementById('endmm').value;


  $.getJSON(
    'https://www.playlogurl.com/playlog.php&date=' + date,
    function(data) {

      var items = [];
      $.each(data, function(key) {
        items.push(
          "<tr id='" +
            key +
            "'><td>" +
            data[key].date +
            '</td>' +
            '<td>' +
            data[key].artist +
            '</td>' +
            '<td>' +
            data[key].song +
            '</td>' +
            '<td>' +
            data[key].length +
             '</td>' +
            '</tr>'
        );
      });

      $('<tbody/>', {
        class: 'list',
        html: items.join('')
      }).appendTo('.my-new-list');
    }
  );
});

HTML snippets:
  <input type="text" id="starthh" /> :
  <input type="text" id="startmm" />
  <input type="text" id="endhh" /> :
  <input type="text" id="endmm" /> 

  <button class="gettracks">Get Tracks</button>
  <table class="my-new-list"></table>


JSON:
{
    "0": {
        "date": "2019-01-28 07:56:34",
        "artist": "Michal Jackson",
        "song": "Beat It",
        "album": "Beat It",
        "length": "4:48",
         ... other values
    },
    "1": {
        "date": "2019-01-28 08:12:39",
        "artist": "Iron Maiden",
        "song": "Run To The Hills",
        "album": "Trooper",
        "length": "4:28",
         ... other values
    },
   ...
    "30": {
        "date": "2019-01-28 12:02:39",
        "artist": "Ministry",
        "song": "No Glory",
        "album": "Trooper",
        "length": "4:28",
         ... other values
    },
    "31": {
        "date": "2019-01-28 12:15:03",
        "artist": "Killing Joke",
        "song": "Pylon",
        "album": "Euphoria",
        "length": "4:48",
         ... other values
    },
....
}

I would like to push to html only the songs that have played between the start and end times from html inputs "starthh" & "endhh" (12:00-16:00 for example). 我想仅将在html输入“ starthh”和“ endhh”(例如12:00-16:00)中在开始时间和结束时间之间播放的歌曲推入html。

I figured it out. 我想到了。 Not sure if it's the best solution but seems to work. 不知道这是否是最好的解决方案,但似乎可行。 Just added this to the beginning of $each-loop: 刚刚将其添加到$ each-loop的开头:

    $.each(data, function(key) {
    let hoursfirst = data[key].date.charAt(11);
    let hourssecond = data[key].date.charAt(12);
    let hours = hoursfirst + hourssecond;
    hours = parseInt(hours);

    if (hours < starthh || hours >= endhh) {
      return;
    }
   ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM