使用节点 js 按名称过滤 [英] filtered by name using node js

查看:54
本文介绍了使用节点 js 按名称过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法可以过滤给定扩展名的文件,然后进一步过滤它们例如:我有 .txt 扩展名,我想从数组中获取我所有的 .txt

Is there any way i can filter files with given extension and then further filter them for eg: I have .txt extension and i want to get all my .txt from an array

file= 
[ "animal_bio.txt",
  "xray.pdf",
  "fish_bio.txt",
  "mammal_doc.txt",
  "human_bio.txt",
  "machine.jpg"
]

过滤后的输出包含所有 .txt 扩展名,并且应该包含所有包含 _bio.txt 名称的文件.

filtered output contain all .txt extension and further it should contain all the files which have _bio.txt name in it.

所以输出看起来像

futherFile=
[ "human_bio.txt",
  "fish_bio.txt",
  "animal_bio.txt"
]

推荐答案

您可以使用 String.protytype.endsWith 函数将字符串与您的扩展名进行比较

You can use String.protytype.endsWith function to compare the strings with your extension

const file = 
[ "animal_bio.txt",
  "xray.pdf",
  "fish_bio.txt",
  "mammal_doc.txt",
  "human_bio.txt",
  "machine.jpg"
]
result = file.filter((fileName) => fileName.endsWith("_bio.txt"));
console.log(result)

这篇关于使用节点 js 按名称过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆