最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Array filtering by search query - Stack Overflow

programmeradmin0浏览0评论

How to filter an array with an particular search term? for example-- we have an array [Tom Harry,Tom John,John Glen,Tom Harward] When we search for "Tom H" then only Tom Harry and Tom Harward should be come as output [Tom Harward,Tom Harry]; Using Javascript

var k=0;
filter=[];
var fc=input.charAt(0);
var sc=input.charAt(1);
for(var i=0;i<array.length;i++)
{
    if(array[i].charAt(0)===fc)
    {
        if(array[i].charAt(1)===sc || sc.charAt(1)=="" )
        {       
            filter[k]=[];
            filter[k]=array[i];
            filter[k]=array[i];
            k++;    
        }
        else
        {
            continue;
        }
    }
    else
    {
        continue;
    }
}

How to filter an array with an particular search term? for example-- we have an array [Tom Harry,Tom John,John Glen,Tom Harward] When we search for "Tom H" then only Tom Harry and Tom Harward should be come as output [Tom Harward,Tom Harry]; Using Javascript

var k=0;
filter=[];
var fc=input.charAt(0);
var sc=input.charAt(1);
for(var i=0;i<array.length;i++)
{
    if(array[i].charAt(0)===fc)
    {
        if(array[i].charAt(1)===sc || sc.charAt(1)=="" )
        {       
            filter[k]=[];
            filter[k]=array[i];
            filter[k]=array[i];
            k++;    
        }
        else
        {
            continue;
        }
    }
    else
    {
        continue;
    }
}
Share Improve this question edited Jan 1, 2013 at 8:44 pkachhia 1,9321 gold badge19 silver badges30 bronze badges asked Jan 1, 2013 at 8:16 Parekh ShahParekh Shah 692 gold badges5 silver badges9 bronze badges 4
  • 2 And have you tried anything? What happened, or went wrong? Where are you stuck? Please at least format your code (currently it's a syntax error, assuming you meant to format it as a JavaScript array?), and take a look at the Mark-down help pages, it'll help your questions look better, and likely get you more help. Further to your comment: how did you search? Show us your code. – David Thomas Commented Jan 1, 2013 at 8:18
  • I searched for the term only for Tom. But i have to search after the space also not the first word :( – Parekh Shah Commented Jan 1, 2013 at 8:20
  • var k=0; filter=[]; var fc=input.charAt(0); var sc=input.charAt(1); for(var i=0;i<array.length;i++) { if(array[i].charAt(0)===fc) { if(array[i].charAt(1)===sc || sc.charAt(1)=="" ) { filter[k]=[]; filter[k]=array[i]; filter[k]=array[i]; k++; } else { continue; } } else { continue; } } – Parekh Shah Commented Jan 1, 2013 at 8:24
  • Please edit your code into your question, it's practically illegible here in the comments. And, again, please: read the mark-down help pages. – David Thomas Commented Jan 1, 2013 at 8:25
Add a comment  | 

5 Answers 5

Reset to default 8

If you are working on browsers supporting JavaScript 1.6, Array.filter is your choice. Your problem can be solved by the following one-liner(surely you can generalize it by introducing a function with a search_pattern parameter):

['Tom Harry','Tom John','John Glen','Tom Harward'].filter(function(name){return name.match(/Tom H/);});

If you'd like it also works on old browsers like IE8 or below, it's easy to implement filter yourself(The above link contains an implementation). BTW, it's advisable to take use of underscore library which provides many collection manipulation function including filter.

ES6 Implementation, with arrow function and startsWith:

['Tom Harry','Tom John','John Glen','Tom Harward'].filter(name => name.startsWith('Tom H'));

You could use the search() method inside of a for loop.

Info on the search() method: http://www.w3schools.com/jsref/jsref_search.asp

Example:

for(var i=0; i < nameOfArray.length; i++){
    if(nameOfArray[i].search('Tom H') > -1){
       resultArray.push(nameOfArray[i]); 
    }
}

This code will loop through every element in your array and if the text you entered matches partially what you typed (via the search method) it will push the item from the array into a results array. This should create an array that has all matching results.

You can check the String.match() or the String.indexOf() methods.

var s="Tom Harry"
 if (s.match(/Tom H.*/)) {
  // do something
}

or you can use indexof(),just like

s.indexOf("Tom H") 

and then check the index. If it's >= 0, it contains that string.

 <script type="text/javascript">

  var src1= ["Tom Harry","Tom John","John Glen","Tom Harward"];

alert(searchStringInArray (src1,'Tom H') );


function searchStringInArray (src1,searched) 
{
  var res=""
  for (var j=0; j<src1.length; j++) {
    if (src1[j].match (searched)) 
    {
    res=res+src1[j]+","
    }
}
return res;
} 
</script>

use this mate

发布评论

评论列表(0)

  1. 暂无评论