当前位置:网站首页>JS written test question -- realize the flat function of array
JS written test question -- realize the flat function of array
2022-07-25 02:49:00 【H5_ ljy】
Array.prototype.myflat=function(count){
let arr=[];
// Traversing the members of an array
for(let i=0;i<this.length;i++){
// Determine whether members are arrays
if(this[i].constructor==Array&&count>0){ // When the dimension is <=0 Then stop
// If it is an array, use the idea of function self calling , Cycle again
let arr1=this[i].myflat(count-1) // here count It represents the descending dimension
// Ergodic out arr1 Members of , Put in what we return arr in Because we call the function to return an array, we have to traverse
arr.push(...arr1)
}else{arr.push(this[i])}
}
return arr;
}
var arr=[[1,2,3],8,9,[[1,2],[2,3]],[[[1,2]]]]
console.log(arr.myflat(3))

边栏推荐
- Jenkins configuration plug-in interface displays "suggestions collection" in Chinese
- Details of happens before rules
- Beginners must see the markdown User Guide
- Is it necessary to increase the number of milliseconds and save several KB of memory in the program?
- Redis unauthorized access vulnerability recurrence (www.hetianlab.com)
- How to switch terminators in PostgreSQL?
- It7259q-13, it7259ex-24 feature wearable devices
- What are the basic skills of engineers? How to practice? -- Learning experience sharing "suggestions collection"
- Remote sensing image classification tool and visualization application of WebGIS
- Several dpdk control frameworks
猜你喜欢

Wechat sports field reservation of the finished works of the applet graduation project (7) mid-term inspection report

Study notes of filebeat

Componentization and modularization

Conceptual distinction between Po, Bo, VO, dto and POJO

What should I do when the interface encounters jsonstring

UDP message structure and precautions

Sword finger offer 11. rotate the minimum number of the array

Flutter apple native Pinyin keyboard input exception on textfield | Pinyin input process callback problem

Cookies and sessions

Apk packaging process
随机推荐
On Calc optimization of calcite
JS foundation -- math
Experienced the troubleshooting and solution process of an online CPU100% and the application of oom
Common Oracle commands
BGP introduction
Request and response
Wechat sports field reservation of applet completion works applet graduation design (8) graduation design thesis template
How to communicate with aliens
Matlab draws radar chart (four lines of code)
SQL recursive follow-up
Keepalivetime=0 description of ThreadPoolExecutor
Digital commerce cloud fine chemical industry management platform integrated informatization solution
"Introduction to interface testing" punch in to learn day05: how can a testing framework support restful interfaces?
Project management tool Zen
Beginners must see the markdown User Guide
Ten year structure and five-year Life-03 trouble as a technical team leader
Map set learning
Picgo configuring Alibaba cloud OSS
Class notes (4) (2) -- 572. Compete
"Introduction to interface testing" punch in day06: interface testing platform: are tools and frameworks incompatible?