查找一个名称数组中包含了多少次字符

发布于 2025-02-14 00:58:09 字数 967 浏览 2 评论 0原文

我正在尝试在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

几度春秋 2025-02-21 00:58:09
const stringArr = [
  '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'
]

const result = stringArr
  .map(item => item.split('l').length - 1)
  .reduce((acc, value) => acc + value, 0)

console.log(result)

const stringArr = [
  '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'
]

const result = stringArr
  .map(item => item.split('l').length - 1)
  .reduce((acc, value) => acc + value, 0)

console.log(result)

月竹挽风 2025-02-21 00:58:09

尝试以下操作:

myStr.split('<my_char>').length - 1. 

每次您看到所需的字符时,都会拆分字符串,并在定界线减去1分裂后返回字符串数,这正是您想要的。在将字符串数组转换为单个字符串后执行此操作。 mystr = mystrary.tostring();

Try this:

myStr.split('<my_char>').length - 1. 

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();

我只土不豪 2025-02-21 00:58:09

这是一种使用阵列方法” noreferrer'> foreach和a ...代码>循环

const stringArr = ["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"]


let count = 0;

stringArr.forEach((string) => {
  for (let letter of string) {
    if (letter.toLowerCase() === "l") count += 1;
  }
});

console.log(count);

Here's a way using the array method forEach and a for...of loop

const stringArr = ["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"]


let count = 0;

stringArr.forEach((string) => {
  for (let letter of string) {
    if (letter.toLowerCase() === "l") count += 1;
  }
});

console.log(count);

我三岁 2025-02-21 00:58:09

您可以使用与您要匹配的角色匹配的简单正则正等式,而在数组项目上迭代时。

const arrayToSearch = ["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"];

const REGEX_MATCH = /l/ig;

function getCount(array, regex) {
  const matchCount = array.reduce((memo, item) => {
    const matches = item.match(REGEX_MATCH);

    if (matches && matches.length > 0) {
      memo += matches.length;
    }

    return memo;
  }, 0);

  return matchCount;
}

const count = getCount(arrayToSearch, REGEX_MATCH);

console.log(`count is ${count}`);

You can use a simple regex that matches the character that you want to match with while iterating over the array items.

const arrayToSearch = ["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"];

const REGEX_MATCH = /l/ig;

function getCount(array, regex) {
  const matchCount = array.reduce((memo, item) => {
    const matches = item.match(REGEX_MATCH);

    if (matches && matches.length > 0) {
      memo += matches.length;
    }

    return memo;
  }, 0);

  return matchCount;
}

const count = getCount(arrayToSearch, REGEX_MATCH);

console.log(`count is ${count}`);

波浪屿的海角声 2025-02-21 00:58:09
const data = ["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"]

const howMany = (data, char) => data.join('').match(new RegExp(char, 'g')).length

console.log(howMany(data, 'p'))

const data = ["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"]

const howMany = (data, char) => data.join('').match(new RegExp(char, 'g')).length

console.log(howMany(data, 'p'))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文