gpt4 book ai didi

How to create an array that start and ends with given numbers?(如何创建以给定数字开始和结束的数组?)

转载 作者:bug小助手 更新时间:2023-10-25 23:11:12 27 4
gpt4 key购买 nike



let start = 2;
let finish = 6;

let arr = Array.from({length: finish}, (_, a) => a + 1);

Is there any way to incorporate the variable start so the array starts at this specific number and increases by 1 until it reaches the length?

有没有办法合并变量Start,使数组从这个特定的数字开始,然后加1,直到它达到长度?


the final result should be an array that starts with start value 2 increases by one and finishes when it reaches finish value 6. [2, 3, 4, 5, 6] in this case

最终结果应该是一个数组,该数组从开始值2开始加1,并在达到结束值6时结束。


更多回答

I recommend trying to understand the Array.from expression that you already have. If you find unclear documentation for that function, please update here,

我建议您尝试理解您已有的Array.from表达式。如果您发现该函数文档不明确,请在此处更新,

What should the final result be?

最终的结果应该是什么呢?

the final result should be an array that starts with start value 2 increases by one and finishes when it reaches finish value 6. [2, 3, 4, 5, 6] in this case!

最终结果应该是一个数组,该数组从开始值2开始加1,并在达到结束值6时结束。

优秀答案推荐

Array.from(
{ length: finish - start },
(_, i) => start + 1 + i
)




const start = 18
const finish = 25
const arr = Array.from({length: finish - start + 1}, (_, a) => a + start);
console.log(arr) // [18, 19, 20, 21, 22, 23, 24, 25]





Two alternatives to Array.from:

Array.from的两个备选方案:


Array(end - start + 1).fill(0).map((_, i) => start + i);

数组(end-start+1).ill(0).map((_,i)=>start+i);


[...Array(end - start + 1)].map((_, i) => start + i);

[...数组(end-start+1)].map((_,i)=>start+i);


Remove the + 1 if end should be exclusive.

如果末端应该是独占的,则删除+1。



Pseudocode:

伪码:


arr = new int[finish - start + 1];

for (int i = 0; i < arr.length; i++) {
arr[i] = start++;
}


You should use your "start" number:

你应该使用你的“开始”号码:


let start = 2
let finish = 7
let arr = Array.from({length: finish}, (_, a) => a + start);

更多回答

"There are only two hard things in Computer Science: cache invalidation, naming things and one-off errors". ;)

计算机科学中只有两件难做的事情:缓存失效、命名事物和一次性错误。;)

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com