js uses Array.from to quickly generate an array of 0~5 with a step value of 0.1

1. The method we usually use is the for loop generation

let data=[]
for(let i=0;i < 51;i++){
    
    
	data.push(i/10)
}

Second, use Array.from to generate

Let's get to know our protagonists today! ! !

1. Interpretation

The Array.from() method creates a new, shallow-copy Array instance from an Array-like or Iterable object.

2. Basic grammar

Array.from(arrayLike[, mapFunction[, thisArg]])

  • arrayLike : required parameter, array-like object or an iterable
  • mapFunction : optional parameter, mapFunction(item, index) { ... } is a function called on each item in the collection, and the returned value is inserted into the new collection.
  • thisArg : optional parameter, the this object when executing the callback function mapFunction, this parameter is rarely used

When converting an object to an array
Please note:
1. There must be a length attribute in the object, and the length of the returned array depends on the length of the length
2. The key must be a value

3. Application

Here we use Array.from to generate a range of numbers

Syntax:
Array.from({length: end}, (_,index) => index)

Using this syntax, we can get the result we want

let data = Array.from({
    
    length:51},(_,i)=>{
    
    
    return i/10
})

For more applications of Array.from, you can see
https://blog.csdn.net/weixin_44447255/article/details/125167451

insert image description here

Guess you like

Origin blog.csdn.net/qq_33235680/article/details/130156477