获取javascript以在数组中搜索数组

获取javascript以在数组中搜索数组,javascript,arrays,search,Javascript,Arrays,Search,我使用以下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

我使用以下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");
mymusic=[{title:“a”,艺术家:“b”,艺术品:“c”,曲目:[{tracktitle:“d”,trackmp3:“e”}];
tracksArray=[];
trackTitles=[];
var评分=0;
var-artistScore=0;
var tracksScore=0;
stringToSearchFor=“d”;
对于(i=0;i请尝试以下方法:

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");
var tracksScore=0;
stringToSearchFor=“d”;

对于(var f=0;f您有许多基本错误,这些错误最终源于变量过多。以下是您的代码重构:-

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");
mymusic=[{title:“a”,艺术家:“b”,艺术品:“c”,曲目:[{tracktitle:“d”,trackmp3:“e”}];
var评分=0;
var-artistScore=0;
var tracksScore=0;
stringToSearchFor=“d”;
for(var i=0;i
您正在调用数组。应为方括号

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

由于已经有了过程方法的答案,这里有一个基于函数式数组处理的答案,以获得额外的乐趣(*):

array.map
array.filter
在ECMAScript第五版中是标准化的,但在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;
    };
}
if(!('map'在Array.prototype中)){
Array.prototype.map=函数(f,that){
var a=新数组(此.length);

对于(var i=0;i请看一看名为的库。它是为此类内容而设计的。这些任务通常归结为一两行易于阅读的代码


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

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

应该是
var f
var g
修复的,谢谢。这是原始代码中的一个错误。我试图在不做太多更改的情况下修复代码,但我想我也可以修复该错误。下次将此拆分为方法。此代码看起来不可读:(
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;
    };
}
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 );