7

I have a node.js script where I want to find if a given string is included inside an array. how can I do this?

Thanks.

2
  • I do not understand your question? You want to find a string inside an array? Please describe your question a little bit better. An commented example would be helpful. Some comments inside your fiddle would be also helpful to find out, what you want to know. Commented Jan 24, 2016 at 13:01
  • edit. please look again if you have time. Commented Jan 24, 2016 at 13:10

3 Answers 3

33

If I understand correctly, you're looking for a string within an array.

One simple way to do this is to use the indexOf() function, which gives you the index a string is found at, or returns -1 when it can't find the string.

Example:

var arr = ['example','foo','bar'];
var str = 'foo';
arr.indexOf(str); //returns 1, because arr[1] == 'foo'

str = 'whatever';
arr.indexOf(str); // returns -1 

Edit 19/10/2017

The introduction of ES6 gives us : arr.includes('str') //true/false

Sign up to request clarification or add additional context in comments.

3 Comments

hey, thank you. but, what if i want the string to be a var from the user? and also, where do i put the code for the match in my function? take a look on the jsfiddle. i am new on node.js , so thanks for your help.
in that case, var str = process.argv[2] should do the trick
try to do that : jsfiddle.net/eyal4/qe0r1z26, and its give me an error. i want it also to print the full str - excectly evrey string that contain this str. its little bit hard i guess, but appreciate your help ! how could i do it clean?
5

In NodeJS, if you are looking to match a partial string in an array of strings you could try this approach:

const strings = ['dogcat', 'catfish']
const matchOn = 'dog'

let matches = strings.filter(s => s.includes(matchOn))

console.log(matches) // ['dogcat']

EDIT: By request how to use in context fiddle provided:

var fs = require('fs');                                           
var location = process.cwd();                                     


var files = getFiles('c:/Program Files/nodejs');                                 
var matches = searchStringInArray('c:/Program Files/nodejs/node_modules/npm/bin', files);


console.log(matches);                                             

function getFiles (dir, files_){                                  
    var str = process.argv[2];                                    
    files_ = files_ || [];                                        
    var files = fs.readdirSync(dir);                              
    for (var i in files){                                         
        var name = dir + '/' + files[i];                          
        if (fs.statSync(name).isDirectory()){                     
            getFiles(name, files_);                               
        } else {                                                  
            files_.push(name);                                    
        }                                                         
    }                                                             

    return files_;                                                
}                                                                 

function searchStringInArray (find, files_) {                     
    return files_.filter(s => s.includes(find))                   

}                                                                 

3 Comments

hey, thank you. but, what if i want the string to be a var from the user? and also, where do i put the code for the match in my function? take a look on the jsfiddle. i am new on node.js , so thanks for your help.
put it in a function for you. I am returning the list of matches and not a number like you had. Not sure if thats what you meant.
ok, thanks, but ive been trying to do that jsfiddle.net/eyal4/qe0r1z26/1 like you said and thats not print nothing. how do i print evrey string that include that string and where do i put that on my code. thanks again
2

Another way is to use some

players = [];

const found = this.players.some(player => player === playerAddress);

if (found) {
    // action
} else {
    // action
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.