当前位置:网站首页>For and for... In, for each and map and for of

For and for... In, for each and map and for of

2020-11-09 10:53:00 Drink the sheep

//for And for...in The difference between :
1、 The same thing :for It can traverse arrays with fixed length and fixed number of times .for...in You can also traverse arrays , But it can also traverse objects .
for(var k in Array name ) //k=key( Subscript )
2、 Difference :for It's the number of times to execute according to the length of the data ,empty( empty ) Will perform . and for..in Then the number of times will be executed according to the data content ,empty Will skip and not execute .

When traversing an array or object, use for...in
//for each Just do the traversal , No return value , Printing is undifined, Just do traversal function ;
//map It can also be used for array traversal , But you can return an array , But be sure to return;

//for of  Traversal array , Can't traverse object 
for let k of arr.keys() // Get key ;
for let v of arr.values()// Get the value ;
for let[k,v] of arr.entries// Get both the key and the value ;
//for of  Traversing objects 
for (let k of object.keys(obj))// Get the key of the object ;
for (let v of object.values(obj)) // Get the value of the object ;
for (let v of object.entries(obj)) // Get the key and value of the object ;

版权声明
本文为[Drink the sheep]所创,转载请带上原文链接,感谢