当前位置:网站首页>ES6 array method
ES6 array method
2022-06-29 13:47:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
Array Array
In order to supplement some defects of some methods in the original array ,ES6 Add a lot more in the array API Such as Array.from include fill wait .
Array.from()
The API It can be used to convert class arrays and convenient objects to arrays , such as function Medium arguments object ( An array of class ),set map es6 New traversable objects
function test () {
var arr = Array.from(arguments);
console.log(arr);
}
test(1,2,3); // [1,2,3]
For browsers that are not yet compatible with this method, you can use slice Instead of
[].slice.call(obj) // The same result will be returned
TIP
This function can also accept the second parameter , Be similar to map Method For each element
Array.from(obj,(i) => i*i) // For each element squared Array,of()
Used to convert a set of numbers into an array
var arr = Array.of(1,2,3,4) // [1,2,3,4]Of the array instance find And findIndex
find Method is used to find the first qualified on the array value , And return it
such as [1,2,5,6,2,4];
We want to find the first... On this array value Greater than 5 Number of numbers
[1,2,5,6,2,4].find((item) => item > 5) // 6
If you find the last value that does not meet the condition So return undefined
findIndex Used to return the subscript of the first value in the array that satisfies the condition
[1,2,5,6,2,4].find((item) => item > 5) // 3
If you find the last one that doesn't meet the conditions , Then the return -1
And very important , These two functions can actually NaN 了
[NaN].find((item) => Object.is(NaN,item)) // NaNArray instance fill
fill(item,start,end)
Give a value , Use this value to fill the entire array , This function can also take two arguments , A starting position , An end position
When you use this function to fill in , When only the first parameter is given , Will directly fill the entire array , Whether or not there is a value at a certain position Of the array instance entries keys values
entries => [key,value]
keys => key Subscript
values => value value
for(let [index,value] of arr.entries()){
console.log(index,value)
}Of the array instance includes
Used to detect whether an array contains a value And back to One bool value
include(item,st=0)
The first parameter is the value used to detect
The second parameter is the starting position of the probe The default is 0 Start
If the browser does not support this method , It can also be used. some Method Instead of Array derivation ES7
var a1 = [1,2,3,4]
var a2 = [for (i of a1) i*2] // [2,4,6,8]Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/132369.html Link to the original text :https://javaforall.cn
边栏推荐
- Yyds dry inventory solution sword finger offer: find the nearest common ancestor of two nodes in the binary tree
- Pod security policy (PSP)
- #yyds干货盘点# 解决剑指offer:在二叉树中找到两个节点的最近公共祖先
- Win32 Tetris (essential for learning MFC)
- Prometheus 2.28.0 新特性
- Write it down once Net analysis of a property management background service stuck
- The former security director of Uber faced fraud allegations and concealed the data leakage event
- 别再重复造轮子了,推荐使用 Google Guava 开源工具类库,真心强大!
- 自主可控再下一城!首套国产ARTIQ架构量子计算测控系统发布
- Xiaobai learns MySQL - incremental statistical SQL requirements - windowing function scheme
猜你喜欢

Use Gerrit + Zadig to realize trunk development and trunk publishing (including byte flying Book Practice)

Repoptimizer: it's actually repvgg2

在线文本过滤小于指定长度工具

Leetcode game 299

Detailed explanation of PDB symbol library files

打造一个 API 快速开发平台,牛逼!

Windbg调试工具介绍

What if the excel table exported from the repair record is too large?

matplotlib的imshow函数显示灰度图像要设置vmin和vmax2个参数

自主可控再下一城!首套国产ARTIQ架构量子计算测控系统发布
随机推荐
云原生(三十一) | Kubernetes篇之Kubernetes平台基本预装资源
Prometheus 2.28.0 new features
MySQL tuning
Cvpr2022 𞓜 future transformer with long-term action expectation
Shell judges whether the command is executed successfully
使用 Gerrit + Zadig 实现主干开发主干发布(含字节飞书实践)
C语言内存函数
How to make dapper support dateonly type
Another "provincial capital university", coming!
GBase8s数据库遇到951错误是什么原因?
Create an API rapid development platform, awesome!
GEE——美国LANDFIRE火灾数据集
[system design] proximity service
运动App如何实现端侧后台保活,让运动记录更完整?
Yyds dry inventory solution sword finger offer: find the nearest common ancestor of two nodes in the binary tree
Summary of common MySQL statements and commands
Cloud native (31) | kubernetes chapter kubernetes platform basic pre installed resources
*Clock in algorithm *leetcode 146 Analysis of LRU cache algorithm
极光 · 哈夫曼树の生成(线段树结构 非指针)(仿邻接表)
力扣:合并两个有序链表