小编Ale*_*hev的帖子

如何计算 TypeScript 中字符串数组中字符串出现的次数?

我需要迭代一个字符串数组,计算每个字符串的出现次数,并返回一个包含值和出现次数的对象。

我正在尝试使用数组归约函数来实现这样的事情,但是,尽管进行了尝试,但我还无法实现这一点。

所以我有以下内容:

["tony", "tony", "tony", "tony", "kassandra", "tony", "tony", "kassandra"]
Run Code Online (Sandbox Code Playgroud)

我需要输出:

[{ name: "tony", matches: 6 }, { name: "kassandra", matches: 2 }]
Run Code Online (Sandbox Code Playgroud)

我应该使用什么来获得上述输出?

我已经尝试过以下方法:

const names = nameList.map((user) => user.name);

const reduce = names.reduce((p, c) => {
  if (p == c) {
    return { name: p, matches: `idk what put here yet` };
  }
  return c;
});
Run Code Online (Sandbox Code Playgroud)

javascript reduce typescript

3
推荐指数
1
解决办法
507
查看次数

当 rollup.js 中有多个输入时,如何自定义输出文件名?

配置

\n
  {\n    input: ['src/index.js', 'src/module1/index.js', 'src/module2/index.js'],\n    output: {\n      dir: 'dist',\n      format: 'es'\n    }\n  }\n
Run Code Online (Sandbox Code Playgroud)\n

结构

\n
\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 dist\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 index.js\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 index2.js\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 index3.js\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 rollup.config.js\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 src\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 index.js\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 module1\n    \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 foo.js\n    \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 index.js\n    \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 module2\n        \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 bar.js\n        \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 index.js\n
Run Code Online (Sandbox Code Playgroud)\n

期望输出是这样的

\n
\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 dist\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 libname.js\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 module\n\xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 module1.js\n\xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 module2.js\n
Run Code Online (Sandbox Code Playgroud)\n

有什么选项或插件可以提供帮助吗?多谢!

\n

rollup rollupjs

0
推荐指数
1
解决办法
1154
查看次数

标签 统计

javascript ×1

reduce ×1

rollup ×1

rollupjs ×1

typescript ×1