查找与搜索匹配的歌曲(艺术家和/或曲目名称)

时间:2012-06-12 18:20:33

标签: javascript jquery search playlist

我正在使用搜索功能,找到搜索与艺术家姓名和曲目名称匹配的歌曲。如果这个主题让您感兴趣并且有一个理想的方法来解决这个问题,那么您将非常感谢您这更像是一个逻辑问题,而不是编程知识问题。

这是我目前的代码。

searchPlaylist: function(a,b){
    var retArr = [];
    if (typeof a === 'string') a = eval(a);
    $.each(b, function(i,v){
        if (typeof v['id'] !== "string" || typeof v['artist'] !== "string" || typeof v['track'] !== "string") return;
        var artist = v['artist'].replace(/[^a-zA-Z 0-9]+/g,'').replace('   ',' ').replace('  ',' ');
        var track = v['track'].replace(/[^a-zA-Z 0-9]+/g,'').replace('   ',' ').replace('  ',' ');
        if (artist.match(a) || track.match(a)) retArr.push(i);
    });
    aC.searchLength = retArr.length;
    return retArr;
}

以下是我如何调用该函数。

doSearch: function(){
    var val = $.trim($("#sB").val().replace(/[^a-zA-Z 0-9]+/g,'').replace('   ',' ').replace('  ',' '));
    if (1 < val.length && val != search) {
        aC.search = val;
        var rlength = aC.searchLength;
        aC.searchResults = aC.searchPlaylist("/" + aC.search + "/i", aC.playlist);
        if (aC.searchLength > 0) $("#sresultcount").text(aC.searchLength);
        if (rlength != aC.searchLength) {
            aC.loadPlaylist($.grep(aC.playlist, function(v,i){
                return $.inArray(i,aC.searchResults) > -1;
            }));
        }           
    } else if (0 == val.length) {
        aC.search = "";
        aC.searchLength = 0;
        $("#sresultcount").empty();
        aC.loadPlaylist(aC.playlist);
    }
}

aC.searchPlaylist函数传递一个RegEx语句用于搜索,第二个参数是存储播放列表的数组的变量。

这是播放列表数组的结构。

aC.playlist = [
    {"id":"bd6ve0ydHVo","artist":"0SM","track":"The Landing feat. Alex G - Original Mix","img":"http://i.ytimg.com/vi/bd6ve0ydHVo/default.jpg","duration":322},
    {"id":"KIijaPllLNI","artist":"2 Chainz","track":"No Lie - Explicit Version","img":"http://i.ytimg.com/vi/KIijaPllLNI/default.jpg","duration":240},
    {"id":"esBlVulbkQQ","artist":"2 LIVE CREW","track":"We Want Some P--sy","img":"http://i.ytimg.com/vi/esBlVulbkQQ/default.jpg","duration":170},
    {"id":"5sc_nQiuDN0","artist":"2 LIVE CREW","track":"Face Down A-- Up","img":"http://i.ytimg.com/vi/5sc_nQiuDN0/default.jpg","duration":115},
    {"id":"42vxicGNumM","artist":"2 LIVE CREW","track":"Me So Horny","img":"http://i.ytimg.com/vi/42vxicGNumM/default.jpg","duration":284},
    {"id":"42boE4fc5X4","artist":"2 LIVE CREW","track":"Hoochie Mama","img":"http://i.ytimg.com/vi/42boE4fc5X4/default.jpg","duration":180},
    {"id":"sNRa1M39RRY","artist":"2Pac","track":"I Ain't Mad At Cha","img":"http://i.ytimg.com/vi/sNRa1M39RRY/default.jpg","duration":233},
    {"id":"2cjv7hEAytU","artist":"2Pac","track":"Me Against The World","img":"http://i.ytimg.com/vi/2cjv7hEAytU/default.jpg","duration":283},
    {"id":"8p9jSRxguAA","artist":"2Pac","track":"Ambitionz Az A Ridah","img":"http://i.ytimg.com/vi/8p9jSRxguAA/default.jpg","duration":276},
    {"id":"W69SSLfRJho","artist":"2Pac","track":"Life Goes On","img":"http://i.ytimg.com/vi/W69SSLfRJho/default.jpg","duration":302},
    {"id":"W6S7dAsIzIU","artist":"2Pac","track":"All Eyez On Me","img":"http://i.ytimg.com/vi/W6S7dAsIzIU/default.jpg","duration":318},
    {"id":"khkx7yXzGhc","artist":"2Pac","track":"2 Of Amerikaz Most Wanted - (Explicit)","img":"http://i.ytimg.com/vi/khkx7yXzGhc/default.jpg","duration":316}
];

以下是我认为结果应该是什么。

搜索字词可以是以下之一:

现场工作人员 - 想要一些 要么 现场工作人员需要一些

我通常更喜欢第二个选项,因为我删除了特殊字符,因此第一个示例中的连字符将被删除。

一旦你将字符串分成基于空格的数组,你可以告诉这个困境我们如何搜索艺术家或曲目名称。 (我认为搜索所有可用的组合并不是一个好主意。)

HnS Music Discovery

提前致谢。您的意见将不胜感激。

1 个答案:

答案 0 :(得分:0)

searchPlaylist: function(a,b){
    var retArr = [], search = a.split(' ');
    $.each(b, function(i,v){
        if (typeof v['id'] !== "string" || typeof v['artist'] !== "string" || typeof v['track'] !== "string") return;
        var artist = v['artist'].replace(/[^a-zA-Z 0-9]+/g,'').replace('   ',' ').replace('  ',' ');
        var track = v['track'].replace(/[^a-zA-Z 0-9]+/g,'').replace('   ',' ').replace('  ',' ');
        var song = artist + " " + track;
        var rg = new RegExp('^(?=.*?'+search.join(")(?=.*?")+')',"i");
        if (song.match(rg)) retArr.push(i);
    });
    aC.searchLength = retArr.length;
    return retArr;
}