Buscar canción que coincida con la búsqueda (artista y/o nombre de la pista)

I'm working on a search function that finds the song where the search matches the artist name and the track name. If this topic interests you and have an ideal method of solving this your input would be greatly appreciated. This is more of a logic question than a programming knowledge question.

Aquí está mi código actual.

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;
}

And here is how I call the function.

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);
    }
}

The aC.searchPlaylist function is passed a RegEx statement for the search and the second argument is the variable of the array where the playlist is stored.

Here is the structure of the playlist array.

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}
];

Here is what I'm thinking the results should be.

The search term could be one of the following:

live crew - want some OR live crew want some

I generally prefer the second option because I remove special characters so the hyphen in the first example would get removed.

As you can tell the dilemma once you split the string up into an array based on spaces how do we search in artist or track name. (I don't think it's a good idea to search all available combinations.)

HnS Music Discovery

Thanks in advance. Your opinion will be appreciated.

preguntado el 12 de junio de 12 a las 19:06

¿Cuál es exactamente su pregunta? -

Search for the first word, assemble an array of matches. Then search for the next word within that array and throw out all non-matches. Repeat until all words are searched. -

I'm working on a search function that finds the song where the search matches the artist name and the track name. Right now if you search for "live crew want some" no results will come up. I'm pondering a method by which I can search and it will result in matching the artist 2 LIVE CREW along with its song "We Want Some P--sy". Hope that clarifies my question. -

@Blazemonger thanks for the suggest. I'll use that if we don't come up with a cooler solution. I was hoping for a solution that used RegEx or a solution without loops. -

@Blazemonger I think I've finally figured out why I've been apprehensive on that method. Say you search for "crew live". If you first search for crew you'll have them four results. Then if you search within them results for live it'll match the same four results. Effectively having no regard for the order. -

1 Respuestas

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;
}

Respondido el 15 de junio de 12 a las 11:06

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.