Comparative js common mode loop

for in  VS  for of

1、for in 取 key; for of 取 value;

2, for of array traversal can only be used, for in it can also be used to traverse the object attributes;

3, the same array is convenient, because a property, for in the element can only traverse the array index value, and can iterate for of all the array elements;

4, for in value can iterate the value is manually added or the prototype array, but only for of traversing the array change value;

5, for ... of for ... in loop and can be used with break, continue, out of the loop, return can not be used, being given;

6, whether it is for ... in or for ... of the value can not traverse Symbol type traverse needed value Symbol type Object.getOwnPropertySymbols () method;

 

forEach VS map VS $.each

1, forEach () return value is undefined, the chain may not call;

2, map () Returns a new array, the array does not change the original;

3, there is no way to terminate or out of forEach () loop, unless an exception is thrown, use the break error, use the return will jump out of this cycle, continue to the next cycle;

. 4, $ each () method is defined as a function of a predetermined operation of each element match, return true if skipped out all through the circulation loop return false;

5, parameters only $ .each (key, value), others are value, key form;

6, the last parameter is the callback function forEach current array itself, its second callback function is executed, the this point, the default is this;

. 7, map () must have a return value, otherwise the value map comprising the length of the array undefind;

 

 

Guess you like

Origin www.cnblogs.com/xudandan/p/11925062.html