簡體   English   中英

如何按日期和時間播放最新歌曲

[英]How to play the most recent song by date and time

我正在使用WebApp播放我在工作時從Twitter解析的音樂。 目前,我正在按從SoundCloud獲得的歌曲ID存儲歌曲。 但是我無法組織它播放最后播放的歌曲。 我考慮將數據庫中的密鑰的更改方式更改為日期,但是隨后我將不得不檢查歌曲是否已經存在,並且我認為這樣做很難。 但我想弄清楚如何比較日期,然后顯示最新的歌曲。

我的數據如下所示:

"6655480" : {
      "Date" : "12/16/2014 2:58:39 am",
      "SongName" : "The Island (Steve Angello, An21 & Max Vangeli remix)",
      "Tweet" : "Pendulum/Steve Angello/An21/Vangeli - The Island playing on #BPM - @sxmElectro",
      "uri" : "https://api.soundcloud.com/tracks/6655480"
    }

我嘗試了這個:

$scope.Song.sort(function(a,b){
var c = new Date(a.Date);
var d = new Date(b.Date);
return c-d;
});

但這是行不通的。 任何幫助表示贊賞。

我得到$ scope.Song像這樣:

var ref = new Firebase("https://app.firebaseio.com/SC");
var sync = $firebase(ref);
$scope.Song = sync.$asArray();

更新:

因此,我嘗試通過以下方法解決此問題:

    $scope.log = [];
    angular.forEach(values, function(value, key) {
      value.sort(function(a,b){
            var c = new Date(a.Date);
            var d = new Date(b.Date);
            return c-d;
      }
    }, log);

但是現在沒有任何效果。 我的語法有什么問題? 這是這樣做的正確方法嗎?

簡單的獲取方式

如果您只需要獲取最新的歌曲而無需任何排序和過濾,則只需循環對象並選擇最新的歌曲即可:

 var songs = { "6655480": { "Date": "12/16/2014 2:58:39 am", "SongName": "The Island (Steve Angello, An21 & Max Vangeli remix)", "Tweet": "Pendulum/Steve Angello/An21/Vangeli - The Island playing on #BPM - @sxmElectro", "uri": "https://api.soundcloud.com/tracks/6655480" }, "234": { "Date": "12/13/2014 2:58:39 am", "SongName": "Some Older Song", "Tweet": "Pendulum/Steve Angello/An21/Vangeli - The Island playing on #BPM - @sxmElectro", "uri": "https://api.soundcloud.com/tracks/6655480" }, "123": { "Date": "12/19/2014 2:58:39 am", "SongName": "Some Newer Song", "Tweet": "Pendulum/Steve Angello/An21/Vangeli - The Island playing on #BPM - @sxmElectro", "uri": "https://api.soundcloud.com/tracks/6655480" }, "253252": { "Date": "12/24/2014 2:58:39 am", "SongName": "The Newest Song", "Tweet": "Pendulum/Steve Angello/An21/Vangeli - The Island playing on #BPM - @sxmElectro", "uri": "https://api.soundcloud.com/tracks/6655480" } } var newestDate = new Date(0); // Jan 01 1970 var newestSong; for (var i in songs) { if (new Date(songs[i].Date) > newestDate) { newestDate = new Date(songs[i].Date) newestSong = songs[i]; } } alert('The newest song is: ' + newestSong.SongName); 


ng-repeat過濾方式

最簡單的Angular方法是將ng-repeat與帶有反向參數的orderBy過濾器一起使用:

ng-repeat="song in Song | orderBy:'Date':true"

看到這里: https : //docs.angularjs.org/api/ng/filter/order

注意:由於orderBy過濾器僅適用於數組(而且無論如何不能簡單地對對象進行排序),因此我必須在兩者之間添加一個自定義過濾器,以將對象轉換為數組(使用了https:// stackoverflow中的一個)。 com / a / 19387871/965907 ),因此我們有:

ng-repeat="song in Song | objToArrayCustomFilter | orderBy:'Date':true"

在此處查看其運行情況(添加了一個簡單的切換按鈕來翻轉反向標志):

 angular.module('test', []) .filter('object2Array', function() { return function(input) { var out = []; for (var i in input) { out.push(input[i]); } return out; } }) .controller('Ctrl', function($scope) { $scope.reverse = true; $scope.songs = { "6655480": { "Date": "12/16/2014 2:58:39 am", "SongName": "The Island (Steve Angello, An21 & Max Vangeli remix)", "Tweet": "Pendulum/Steve Angello/An21/Vangeli - The Island playing on #BPM - @sxmElectro", "uri": "https://api.soundcloud.com/tracks/6655480" }, "234": { "Date": "12/13/2014 2:58:39 am", "SongName": "Some Older Song", "Tweet": "Pendulum/Steve Angello/An21/Vangeli - The Island playing on #BPM - @sxmElectro", "uri": "https://api.soundcloud.com/tracks/6655480" }, "123": { "Date": "12/19/2014 2:58:39 am", "SongName": "Some Newer Song", "Tweet": "Pendulum/Steve Angello/An21/Vangeli - The Island playing on #BPM - @sxmElectro", "uri": "https://api.soundcloud.com/tracks/6655480" }, "253252": { "Date": "12/24/2014 2:58:39 am", "SongName": "The Newest Song", "Tweet": "Pendulum/Steve Angello/An21/Vangeli - The Island playing on #BPM - @sxmElectro", "uri": "https://api.soundcloud.com/tracks/6655480" } } }) 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="test" ng-controller="Ctrl"> <p ng-repeat="song in songs | object2Array | orderBy:'Date':reverse"><strong>{{song.SongName}}</strong>: {{song.Date}}</p> <button ng-click="reverse = !reverse">Toggle order</button> </div> 

在Plunker上: http ://plnkr.co/edit/r8oDr5PtsPVZGO5WFaWR?p=preview

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM