用任意值递增填充数组的备用代码

huangapple go评论97阅读模式
英文:

Alternative code for filling an array with a series incremented by any value

问题

以下是已翻译的代码部分:

  1. private seriesIncrementedBy5(upperLimit: number): number[] {
  2. const series = [];
  3. for (let s = 5; s <= upperLimit + 5; s += 5) {
  4. series.push(s);
  5. }
  6. return series;
  7. }

请注意,我只提供了代码的翻译部分,没有其他内容。

英文:

The following typescript function seriesIncrementedBy5 returns an array with numbers starting from 5 to upperLimit with an increment of 5.

Is there a way to replace this 5 lines of code with any recent typescript ES6 advanced function which will take less than 5 lines.

  1. private seriesIncrementedBy5(upperLimit: number): number[] {
  2. const series = [];
  3. for (let s = 5; s &lt;= upperLimit + 5; s += 5) {
  4. series.push(s);
  5. }
  6. return series;
  7. }

答案1

得分: 2

您可以使用 Array.from 方法,该方法可以接受一个带有 length: number 属性和第二个参数用于填充的对象:

  1. const seriesIncrementedBy5 = (upperLimit: number): number[] =>
  2. Array.from({ length: Math.floor(upperLimit / 5) + 1 }, (_, i) => (i + 1) * 5);

为了计算数组的长度,我们将 upperLimit 除以 5 并向下取整得到一个整数;关键是要添加 + 1 以包含 upperLimit 在数组中。对于填充部分,我们将使用元素的索引;因为我们从 5 开始,而不是 0,所以我们将索引加上 + 1,然后乘以 5

英文:

You can use Array.from, which can accept an object with the length: number property and a second argument populating function:

  1. const seriesIncrementedBy5 = (upperLimit: number): number[] =&gt;
  2. Array.from({ length: Math.floor(upperLimit / 5) + 1 }, (_, i) =&gt; (i + 1) * 5);

To calculate the length, we divide the upperLimit by 5 and floor it to get an integer; it is crucial to add + 1 to include the upperLimit in the array. To populate, we will use the element's index; since we are starting from 5, not 0, we will add + 1 to the index and multiply it by 5.

playground

答案2

得分: 1

你可以创建一个正确大小的数组,然后使用 map 进行操作。

  1. const seriesIncrementedBy5 = upperLimit => [...Array((upperLimit/5|0)+1)]
  2. .map((_, i) => 5 * (i + 1));
  3. console.log(seriesIncrementedBy5(10));
  4. console.log(seriesIncrementedBy5(9));
英文:

You can create an array of the correct size and then map over it.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. const seriesIncrementedBy5 = upperLimit =&gt; [...Array((upperLimit/5|0)+1)]
  2. .map((_, i) =&gt; 5 * (i + 1));
  3. console.log(seriesIncrementedBy5(10));
  4. console.log(seriesIncrementedBy5(9));

<!-- end snippet -->

答案3

得分: 1

你可以使用 Array.from 方法。

  1. private seriesIncrementedBy5(upperLimit: number): number[] {
  2. return Array.from({ length: Math.floor(upperLimit / 5) }, (_, i) => (i + 1) * 5);
  3. }

更多信息可以查看这里

英文:

You can use the Array.from method.

  1. private seriesIncrementedBy5(upperLimit: number): number[] {
  2. return Array.from({ length: Math.floor(upperLimit / 5) }, (_, i) =&gt; (i + 1) * 5);
  3. }

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from

huangapple
  • 本文由 发表于 2023年5月22日 23:08:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76307565.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定