在 node.js 中在数组上查找字符串的最佳方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34975998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 19:48:28  来源:igfitidea点击:

what is the best way to find string on array in node.js?

arraysnode.jsstringfindindexof

提问by Eyal Cohen

up to jsfidlle my code, asked the same question yestrday but in java. i have a node.js script that run on cmd. i want to find if string that the user give me at the end of the command (with procces.argv[2]), is inclueded in my array how can i do this?

直到 jsfdlle 我的代码,昨天问了同样的问题,但在 Java 中。我有一个在 cmd 上运行的 node.js 脚本。我想查找用户在命令末尾给我的字符串(使用 procces.argv[2])是否包含在我的数组中,我该怎么做?

link - https://jsfiddle.net/mj4761dk/

hope someone can help me. thank you

希望可以有人帮帮我。谢谢你

eyal

艾尔

回答by xShirase

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.

一种简单的方法是使用该indexOf()函数,该函数为您提供找到字符串的索引,或者在找不到字符串时返回 -1。

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

编辑 19/10/2017

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

ES6 的引入给了我们: arr.includes('str') //true/false

回答by chriskelly

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

在 NodeJS 中,如果您想匹配字符串数组中的部分字符串,您可以尝试以下方法:

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

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

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

EDIT:By requesthow 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))                   

}