what is the best way to find string inside array in node.js?
Asked Answered
B

3

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.

Blatant answered 24/1, 2016 at 12:49 Comment(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.Puberulent
edit. please look again if you have time.Blatant
T
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

Travel answered 24/1, 2016 at 13:25 Comment(3)
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.Blatant
in that case, var str = process.argv[2] should do the trickTravel
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?Blatant
V
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))                   

}                                                                 
Vargas answered 24/1, 2016 at 13:36 Comment(3)
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.Blatant
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.Vargas
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 againBlatant
M
2

Another way is to use some

players = [];

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

if (found) {
    // action
} else {
    // action
}
Methinks answered 6/12, 2020 at 3:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.