简体   繁体   中英

How to match a word from array javascript?

i have javascript array. i need to match and select words from this array.

var tomatch = "";

var sets= new Array()
     sets[0]='nnd';
     sets[1]='nndha';
     sets[2]='ch';
     sets[3]='gn';

as a example... when,

  var tomatch = "nn";

it need tomatch with sets[0] & sets[1] and
write out the result.

as when tomatch = "c" , it should match with sets[2] how can i do this ?

there i dont need an oder.
if tomatch = "dh" , it can also be sets[1]

i have not good knowlage about javascript.

How to do this?

indexof("string to match") will return the index of the string or -1 if the string is not found. Simply loop through the array and check if the return value of indexof("string to match") is -1.

sets[i].indexOf("nn")!=-1

Use the following function to return an array of matched words otherwise alert no match :

function find_match(to_match)
{
    match_array=new Array();
    for(i in sets)
    {
        if(sets[i].indexOf(to_match)!=-1)
            match_array.push(sets[i]);
    }
    return (match_array.length==0? alert('No match'): match_array);
}

To find match for say string 'n' you need to call find_match('n') which will return nnd,nndha,gn

var tomatch = "nn";
var sets= new Array()
     sets[0]='nnd';
     sets[1]='nndha';
     sets[2]='ch';
     sets[3]='gn';    
for(var i=0; i < sets.length ; i++){
       if(sets[i].indexof(tomatch) !== -1){
         return sets[i];
       }
    }

Use filter:

> sets.filter(function(x) { return x.indexOf("nn") !== -1; })
[ 'nnd', 'nndha' ]
> sets.filter(function(x) { return x.indexOf("c") !== -1; })
[ 'ch' ]

it should be:

   function isInArray(string){
     return sets.indexOf(string)>-1;      
   }

   alert(isInArray("nn"));

OR with jQuery:

jQuery.inArray("nnd",sets);

it will return the index of "nnd"

This is not as neat as @Nirk's solution (I always forget about Array.filter) but you can replace my test function with Array.filter per his answer:

function test(a, f) {
  var i = 0,
      result = [];

  for (i=0; i<a.length; i++) {
    result.push(f(a[i]));
  }

  return result;
}

function txtMatch(s) {
  var pattern = new RegExp('' + s + '');
  return function(t) {
    return pattern.test(t);
  };
}

var sets= new Array()
sets[0]='nnd';
sets[1]='nndha';
sets[2]='ch';
sets[3]='gn';

var toMatchFirst = test(sets, txtMatch('nn'));
var toMatchSecond = test(sets, txtMatch('c'));

console.log(toMatchFirst.join(', '));
console.log(toMatchSecond.join(', '));

I just created a quick string match function and then loop through the array, testing each, and returning an array of the results.

Thanks all of you, but this is the one i wanted to, i think match cmd is right for my task.

  var sugest = "";
  var matchkeyword = "nn";

  var sets= new Array()
    sets[0]='nnd';
    sets[1]='nndha';
    sets[2]='ch';
    sets[3]='gn';
    sets[4]='nch';
    sets[5]='fu';
    sets[6]='Nui';


for(var i=0; i < sets.length ; i++){    

        if(sets[i].match(matchkeyword) != null) {
                    sugest = sugest + "<br>" + sets[i];
        }
}

 y=document.getElementById("sugdiv");
 y.innerHTML = sugest;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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