如何在数组中生成从 1 到 100 的数字,而无需实际键入从 1 到 100 的每个数字,并且每个数字仅出现一次。我后来需要将生成的数字移动到不同的数组,但我已经弄清楚了那部分。这是我到目前为止所做的
const arr1 = [1,2,3,4,5,6]
const arr2 = []
const ran = () => {
const index = Math.floor(Math.random() * arr1.length);
if(arr1.length>0)
{
arr2.push(arr1[index])
arr1.splice(index, 1)
const display = document.getElementById('array')
display.innerText = ("\nArray 2 elements " + arr2.toString() + "\n Remaining Array 1 elements" + arr1.toString())
}
else
{
document.write("Array is now empty");
}
}
Run Code Online (Sandbox Code Playgroud)
<button onClick=ran()>click</button>
<span id='array' />
Run Code Online (Sandbox Code Playgroud)
在上面的代码片段中,我也显示了 array1 中的元素,但如果数字是从 1 到 100,则不需要这样做
我想知道是否有一种更容易/更清晰的方法来扩展一个整数6
并将其变成一个数组[1, 2, 3, 4, 5, 6]
.
这就是我所拥有的,我讨厌它.
var pages = function(number_int){
var numbers_array = [];
for (var i = 1; i <= number_int; i++) {
numbers_array.push(i);
}
return numbers_array;
};
Run Code Online (Sandbox Code Playgroud)
如果可以,我很乐意使用下划线.
我想使用Array.fill
方法创建一个从 0...499 的序列,例如
const arr = []
for(let i = 0; i < 500; i++)
arr.push(i);
Run Code Online (Sandbox Code Playgroud)
目前,我正在使用Array.from
类似:
const arr = Array.from(Array(500).keys())
Run Code Online (Sandbox Code Playgroud)
但我怎样才能使用它来Array.fill
创建序列呢?有可能吗?
我想用一个数字创建数组。是否可以?
在 array = 60 中,我的意思是 array[1, 2, 3 ...60],但我想要这个数组只有一个数字。
我想要这样的东西。
JavaScript:
let array = 60;
const map1 = array.map(x => console.log(x));
Run Code Online (Sandbox Code Playgroud)
console.log 必须出现 60 次。
我正在尝试制作一个循环组件,以便我可以在特定的时间内循环它的任何子组件。我怎样才能做到这一点?
// Looper
function Looper({ children, array }) {
return (
<div>
{array.map((item) => (
<div key={item}>{children}</div>
))}
</div>
);
}
// It works, but it needs a dummy array that I don't want.
<Looper array={[1, 2, 3, 4, 5]}>
<span>Hello Guys..</span>
</Looper>
Run Code Online (Sandbox Code Playgroud) 我想将我丑陋的代码改进为更干净和简单的东西。如何缩短此代码?
if (this.foo < 2) {
return (this.result = [ { year: 1 } ]);
}
if (this.foo < 3) {
return (this.result = [ { year: 1 }, { year: 2 } ]);
}
if (this.foo < 4) {
return (this.result = [ { year: 1 }, { year: 2 }, { year: 3 } ]);
}
if (this.foo < 5) {
return (this.result = [ { year: 1 }, { year: 2 }, { year: 3 }, { …
Run Code Online (Sandbox Code Playgroud) javascript中的范围数组是否有一个很好的衬里,相当于python的list(range(m, n))
?ES6允许.这是迄今为止我提出的最好的:
[x for(x of (function*(){for(let i=0;i<100;i++) yield i})())]
Run Code Online (Sandbox Code Playgroud) 我想用最现代的方式制作这样的阵列
const nextchunk = [];
nextchunk[0] = [0, 6];
nextchunk[1] = [7, 13];
Run Code Online (Sandbox Code Playgroud)
每个nextchunk必须有7个位置,如图所示.这些代表稍后在代码中的限制查询.所以nextchunk[1]
拿出第7 - 13项,nextchunk[2]
14 - 20出局
我希望能够nextchunk[20]
在运行时调用并让它返回正确的值.
javascript ×8
arrays ×4
ecmascript-6 ×2
array-map ×1
es6-modules ×1
jsx ×1
loops ×1
reactjs ×1