繁体   English   中英

获取javascript搜索数组中的数组

[英]Getting javascript to search an array within an array

我有以下JavaScript可以遍历记录数组,并为每个字段提醒在数组中找到的匹配项数:

    mymusic=[{title:"a",artist:"b",artwork:"c",tracks:[{tracktitle:"d",trackmp3:"e"}]}];
tracksArray=[];
trackTitles=[];
var albumScore=0;
var artistScore=0;
var tracksScore=0;
stringToSearchFor="d";
for(i=0;i<mymusic.length;i++){
    if((mymusic[i].title).match(stringToSearchFor))
        albumScore+=1;
    }
if(albumScore!=0)
    alert(albumScore+" match(es) found in Albums");
else
    alert("No matches found in Albums");
for(d=0;d<mymusic.length;d++){
    if((mymusic[d].artist).match(stringToSearchFor))
        artistScore+=1;
    }
if(artistScore!=0)
    alert(artistScore+" match(es) found in Artists");
else
    alert("No matches found in Artists");
for(f=0;f<mymusic.length;f++){
    tracksArray[f]=mymusic[f].tracks;
    for(g=0;g<tracksArray;g++){
        trackTitles[g]=tracksArray[g].tracktitle;
        }
    for(h=0;h<trackTitles.length;h++){
        if(trackTitles(h).match(stringToSearchFor))
            {
            tracksScore+=1;
            }
        }
    }
if(tracksScore!=0)
    alert(tracksScore+" match(es) found in Tracks");
else
    alert("No matches found in Tracks");

可以在“标题”和“艺术家”记录中正常使用,但即使有匹配项,也始终会为“曲目”记录提醒“未找到匹配项”。 我猜想问题出在trackTitles数组中的嵌套for循环中,但是我看不到可以更改以使其起作用。 有任何想法吗? 谢谢

if(trackTitles(h)

您正在调用数组。 应该是方括号。

您可以将数组处理内容分解为可重用的函数,以提高可读性并减少这些杂散变量的数量。

既然已经有了使用过程方法的答案,那么以下是一种基于函数式数组处理的方法,可以带来更多乐趣(*):

function countItemsContaining(seq, prop, str) {
    return seq.map(itemGetter(prop)).filter(function(s) {
        return s.indexOf(str)!==-1;
    }).length;
}

function itemGetter(prop) {
    return function(o) {
        return o[prop];
    };
}


mymusic= [{title:"a",artist:"b",artwork:"c",tracks:[{tracktitle:"d",trackmp3:"e"}]}];
needle= 'd';

var titleScore= countItemsContaining(mymusic, 'title', needle);
var artistScore= countItemsContaining(mymusic, 'artist', needle);

// Calling concat is a JavaScript idiom to combine a load of lists into one
//
var mytracks= [].concat.apply([], mymusic.map(itemGetter('tracks')));
var tracksScore= countItemsContaining(mytracks, 'tracktitle', needle);

array.maparray.filter在ECMAScript Fifth Edition中已标准化,但在IE中尚不可用,因此为了兼容,您可以按以下方式定义它们:

if (!('map' in Array.prototype)) {
    Array.prototype.map= function(f, that) {
        var a= new Array(this.length);
        for (var i= 0; i<this.length; i++) if (i in this)
            a[i]= f.call(that, this[i], i, this);
        return a;
    };
}

if (!('filter' in Array.prototype)) {
    Array.prototype.filter= function(f, that) {
        var a= [];
        for (var i= 0; i<this.length; i++) if (i in this)
            if (f.call(that, this[i], i, this))
                a.push(this[i]);
        return a;
    };
}

(*:答案中包含的实际乐趣可能有限)

看一下名为underscore.js的库。 它是为这种材料制成的。 这些任务通常归结为一两行易于阅读的代码。

它在可用时使用本机方法,填充缺少的位(取决于浏览器)并且可以链接。 它甚至使内置数组方法可链接。

尝试以下方法:

var tracksScore=0;
stringToSearchFor="d";
for(var f=0;f<mymusic.length;f++){
    var tracksArray=mymusic[f].tracks;
    for(var g=0;g<tracksArray.length;g++) {
        var tracktitle=tracksArray[g].tracktitle;
        if(tracktitle.match(stringToSearchFor))
        {
                tracksScore+=1;
        }
    }
}
if(tracksScore!=0)
    alert(tracksScore+" match(es) found in Tracks");
else
    alert("No matches found in Tracks");

您有许多基本错误,这些错误最终归因于变量过多。 这是重构的代码:

mymusic=[{title:"a",artist:"b",artwork:"c",tracks:[{tracktitle:"d",trackmp3:"e"}]}];
var albumScore=0;
var artistScore=0;
var tracksScore=0;
stringToSearchFor="d";

for (var i=0; i < mymusic.length; i++)
{
    if( mymusic[i].title.match(stringToSearchFor))
        albumScore += 1;

    if( mymusic[i].artist.match(stringToSearchFor))
        artistScore += 1;

    for (var j = 0; j < mymusic[i].tracks.length; j++)
    {
        if (mymusic[i].tracks[j].tracktitle.match(stringToSearchFor))
            tracksScore += 1
    }
}

if (albumScore != 0)
    alert(albumScore + " match(es) found in Albums");
else
    alert("No matches found in Albums");

if (artistScore != 0)
    alert(artistScore + " match(es) found in Artists");
else
    alert("No matches found in Artists");

if (tracksScore != 0)
    alert(tracksScore+" match(es) found in Tracks");
else
    alert("No matches found in Tracks");

AnthonyWJones和bobince所说的(尽管我需要花一些时间来阅读bobince的答案)。

另一种解决方案:看到数据结构的那一刻,我想到了“递归!” 并认为我能提出一个可以与任何(未知)深度级别的任何大小数据结构一起使用的解决方案很有趣。

我不经常编写代码,因此以下内容可能会混入一些不良做法,但它可以起作用:)。 让我知道你的想法。

myMusic=[{title:"a",artist:"b",artwork:"c",year:"d",tracks:[{tracktitle:"d",trackmp3:"e"}]}];

function find_match(dataObj,stringToSearchFor,resultObj){

  resultObj = (resultObj)?resultObj:{}; //init resultObj

  for (dataKey in dataObj){ //loop through dataObj
    if (typeof(dataObj[dataKey]) == "object"){ //if property is array/object, call find_match on property
     resultObj = find_match(dataObj[dataKey],stringToSearchFor,resultObj); 
    }else if (dataObj[dataKey].match(stringToSearchFor)){ //else see if search term matches    
      resultObj[dataKey] = (resultObj[dataKey] )?resultObj[dataKey] +=1:1; //add result to resultObj, init key if not yet found, use dataObj key as resultObj key 
    }
  }

  return resultObj; //return resultObj up the chain

}

results = find_match(myMusic,"d");

alertString = "";

for (resultKey in results){ //loop  through results and construct alert msg.
  alertString += results[resultKey] + " match(es) found in " + resultKey + "\n";
}

alert(alertString );

暂无
暂无

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

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