查找一个名称数组中包含了多少次字符
我正在尝试在JavaScript中进行以下练习:我需要返回字母“ l”的次数在一系列名称中使用。
这是我必须使用的数组的一个示例:
["earth (c-137)","abadango","citadel of ricks","worldender's lair","anatomy park","interdimensional cable","immortality field resort","post-apocalyptic earth","purge planet","venzenulon 7","bepis 9","cronenberg earth","nuptia 4","giant's town","bird world","st. gloopy noops hospital","earth (5-126)","mr. goldenfold's dream","gromflom prime","earth (replacement dimension)","testicle monster dimension","signus 5 expanse","earth (c-500a)","rick's battery microverse","the menagerie","earth (k-83)","hideout planet","unity's planet","dorian 5","earth (unknown dimension)","earth (j19ζ7)","roy: a life well lived","eric stoltz mask earth","earth (evil rick's target dimension)","planet squanch","glaagablaaga","resort planet","interdimensional customs","galactic federation prison","gazorpazorp"]
请注意,字母“ l”可能会在一个名称中重复。
我可以使用任何JS方法吗?我已经尝试了索引,但无法正常工作
I'm trying to do the following exercise in JavaScript: I need to return the number of times the letter 'l' is used inside an array of names.
This is an example of the array I have to use:
["earth (c-137)","abadango","citadel of ricks","worldender's lair","anatomy park","interdimensional cable","immortality field resort","post-apocalyptic earth","purge planet","venzenulon 7","bepis 9","cronenberg earth","nuptia 4","giant's town","bird world","st. gloopy noops hospital","earth (5-126)","mr. goldenfold's dream","gromflom prime","earth (replacement dimension)","testicle monster dimension","signus 5 expanse","earth (c-500a)","rick's battery microverse","the menagerie","earth (k-83)","hideout planet","unity's planet","dorian 5","earth (unknown dimension)","earth (j19ζ7)","roy: a life well lived","eric stoltz mask earth","earth (evil rick's target dimension)","planet squanch","glaagablaaga","resort planet","interdimensional customs","galactic federation prison","gazorpazorp"]
Note that the letter 'l' might be repeted inside a single name.
Is there any JS method I could use to do this ? I've tried with indexOf but couldn't make it work
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试以下操作:
每次您看到所需的字符时,都会拆分字符串,并在定界线减去1分裂后返回字符串数,这正是您想要的。在将字符串数组转换为单个字符串后执行此操作。 mystr = mystrary.tostring();
Try this:
This splits the string everytime you see the char you want and returns the number of strings after splitting at the delimiter minus 1 which is exactly what you want. Do this after converting the array of strings to a single string. myStr = myStrArray.toString();
这是一种使用阵列方法” noreferrer'>
foreach
和a
...代码>
循环
Here's a way using the array method
forEach
and afor...of
loop您可以使用与您要匹配的角色匹配的简单正则正等式,而在数组项目上迭代时。
You can use a simple regex that matches the character that you want to match with while iterating over the array items.