Code example:
let arr = [] // push methodfor(let i = 1,len=100;i<=100;i++){(i)} // or //Cyclic assignmentfor(let i = 1,len=100;i<=100;i++){arr[i-1]=i} // () method There will be a little optimization later({length:101}, (v,k) => k)// or (Array(101), (v,k) =>k); (Array(101).keys()) (0,1) // Use recursive implementation// Current value// Maximum lengthlet arr = [] function setArray(current,maxLength,arr){ if(<maxLength){ arr[current-1] = current setArray(++current,maxLength,arr) }else{ (i=>i)// Remove false values empty } } setArray(1,100,arr) // I don't know if there are any performance issues // I saw some methods online, which are quite fun, but I don't know the source // 1 let arr = Array(100).toString().split(',').map((item,index)=>index)// 0-> 99 // 2 let i = 0; let timer = setInterval(function(){ arr[i] = i++; if(i>=100){ clearInterval(timer); (arr); } },1); // 0-> 99 // 3 ((null, {length:100})).map(item=>+item);// 0-> 99 // 4 [...Array(100).keys()]// 0-> 99 [...({length:100}).keys()]// 0-> 99 // mdn see sequence generator (specified range)const range = (start, stop, step) => ({ length: (stop - start) / step + 1}, (_, i) => + (i * step)); range(1,100,1)// 1-> 100 //Optimize what I wrote above({length:100},(_, i)=>1+(i)) // 1-> 100
What I think looks most comfortable is:({length:100},(_, i)=>1+(i))
Prerequisite knowledge:
()
(arrayLike[, mapFn[, thisArg]])
arrayLike
A pseudo-array object or iterable object that you want to convert to an array.
mapFn
OptionalIf this parameter is specified, each element in the new array executes the callback function.
thisArg
OptionalOptional parameters, execute callback function
mapFn
hourthis
Object.Return value
A new one
Array
Example.
usage:
- String Generate Array
- Set Generate Array
- Map Generate Array
- Class Array Generate Array
Example: Generate an array from a map
const map = new Map([[1, 2], [2, 4], [4, 8]]); (map); // [[1, 2], [2, 4], [4, 8]] const mapper = new Map([['1', 'a'], ['2', 'b']]); (()); // ['a', 'b']; (()); // ['1', '2'];
other:
({length: 100}) or (Array(100)) // Will be generated(100) [undefined,..., undefined]
Array instance method keys()
keys() is a new traversal of key names added in ES6, returning a traversal object
apply(thisArg, [argsArray])
apply(thisArg, [argsArray])
parameter
thisArg
Required. exist
func
Used when function runsthis
value. Please note thatthis
Probably not the actual value seen by the method: if this function is inNon-strict modeNext, specify asnull
orundefined
It will be automatically replaced with a pointing to a global object, and the original value will be wrapped.
argsArray
Optional. An array or class array object, where the array element is passed to
func
function. If the value of this parameter isnull
orundefined
, it means that no parameters need to be passed in. Starting with ECMAScript 5, you can use class array objects.Return value
The call is specified
this
The result of a function with values and parameters.
Supplement: Implement randomly obtaining non-repetitive arrays of 1-100 in js
<script type="text/javascript"> function getArr() { var i = 0; var arr = new Array(); var p; while (i < 100) { //Generate random numbers var p = (() * 100 + 1) if ((p) == -1) { arr[i] = p; i++; } } return arr; } </script>
Summarize
This is the end of this article about how to quickly create a 1 to 100 array using JavaScript. For more related JS content, please search for my previous article or continue browsing the related articles below. I hope you will support me in the future!