当前位置:网站首页>Array method added in ES6
Array method added in ES6
2022-07-28 19:59:00 【ca11meback】
var arr=Array.of(10,20,30)//Array.of(10)
var arr2=new Array(10)
console.log(arr,arr2)new function 1:
of()
function : Set array length
If there is only one parameter, it is the length of the array , If more than one , The length of the array is the number of elements
new function 2:
from()
function : Convert to array
The parameter is the data to be converted into an array, which is in front arr-map-set Common in
let arr = Array.from({
0: '1',
1: '2',
2: 3,
length: 3
});
console.log(arr); // ['1', '2', 3] Note that if you use objects to array
Object's key To convert to the subscript of the array
var arr=[10,203,400,2,3,5,6]
var re=arr.find(function(el){
console.log(111)
if(el>100){return true}
})new function 3:
find()
function : lookup
By traversing the query conditions Stop until you find it Returns the value found
var arr=[1,23,5,56,{name:"bob"}]
var re=arr.includes({name:"bob"})
console.log(re)new function 4:
includes()
function : Does it contain
Returns a Boolean value , Although the contents of the two objects are the same, they are not the same reference , So back false
var arr=[10,30,50]
var re=arr.findIndex(function(el){
if(el>20){return true}
})
console.log(re)new function 5:
findIndex()
The function finds the element and returns the subscript and find Similar, but the return value is the subscript of the element in the array
var arr=[10,203,4,100,3004,5]
var arr2=arr.fill("hello",2,4)//2 3
console.log(arr,arr2,arr==arr2)new function 6:
fill()
function : Replace
The first parameter is the replaced element , The second is the starting position of replacement , The third is the end position of the replacement, but it is the left closed right open interval It is actually the end position -1
var arr=[[10,20,30],40,50,[[60,70],80,[90,[110],100]]]
var arr2=arr.flat(3)
console.log(arr2)new function 7:
flat()
function : Array dimension reduction
The parameter represents the number of dimension reduction
How to create your own method implementation flat function
var arr=[[10,20,30],40,50,[[60,70],80,[90,[110],100]]]
Array.prototype.myflat=function(count=Infinity){
var arr=[]
for(var i=0;i<this.length;i++){
// arr.push(this[i])
if(this[i].constructor==Array&&count>0){
// for(var j=0;j<this[i].length;j++){
// arr.push(this[i][j])
// }
var newarr=this[i].myflat(count-1)
for(var j=0;j<newarr.length;j++){
arr.push(newarr[j])
}
}else{
arr.push(this[i])
}
}
return arr
}
var re2=arr.myflat(1)
console.log(re2)边栏推荐
- Thoroughly understand bit operations -- and (&), not (~), or (|), XOR (^)
- Return and job management of saltstack
- 投资35.45亿元!格力集团参与小米产业基金
- Integration and implementation of login click graphic verification code in personal blog system
- String中常用的API
- 中国能否在元宇宙的未来发展中取得突破,占领高地?
- 软考中级(系统集成项目管理工程师)高频考点
- The opening price soared by 215%! Domestic signal chain chip enterprise Xinhai Technology landed on the scientific innovation board
- Salt SSH of saltstack
- leetcode day5 删除重复的电子邮箱
猜你喜欢

Codeignier framework implements restful API interface programming

Stories of Party members | Li qingai uses cartoons to drive farmers to increase income and become rich

Idea properties file display \u solution of not displaying Chinese

Leetcode Day1 score ranking
![[NPP installation plug-in]](/img/6f/97e53116ec4ebc6a6338d125ddad8b.png)
[NPP installation plug-in]

Deploy LNMP automatically with saltstack

远光软件获得阿里云产品生态集成认证,携手阿里云共建新合作

个人博克系统登录点击图形验证码的集成与实现

Saltstack advanced

数字滤波器设计——Matlab
随机推荐
Stories of Party members | Li qingai uses cartoons to drive farmers to increase income and become rich
一文读懂如何部署具有外部数据库的高可用 K3s
Verification of chip power consumption performance: from dilemma to transcendence
MySQL8 基于clone创建主从复制
Germany and Portugal have announced that they will not disable Huawei 5g equipment, but Germany will set strict restrictions!
CDGA|工业互联网行业怎么做好数据治理?
Why is there no log output in the telnet login interface?
Prometheus deployment
Codeignier framework implements restful API interface programming
Android section 13 03xutils detailed explanation of database framework (addition, deletion and modification)
STC12C5A60S2 function description (STC12C5A60S2 is triggered by default)
The United States will provide $25billion in subsidies to encourage chip manufacturers such as Intel to move back to production lines
Leetcode day3 employees who exceed the manager's income
Overcome the "fear of looking at teeth", and we use technology to change the industry
leetcode day5 删除重复的电子邮箱
并发程序设计,你真的懂吗?
With the help of panel industry innovation, will FPGA become the best choice for TCON?
How openocd directly downloads programs to STM32 board through stlink (solved)
爬取IP
Leetcode day2 连续出现的数字