Js-for in: Used to traverse objects
1 //traversing objects for in2 3 varOpts={name: ' Xiaofei ', Age: ' 28 years old ', Job: ' Web Front end engineer '}4 5 for(varKinchopts) {6 7Console.log (k + ': ' +Opts[k])8 9 }Ten //Name:xiaofei One //age:28 years A //Job:web Front End
Js-foreach: Used to iterate over an array
1 //iterating through an array2 3 vararr=[1,2,3,4];4Arr.foreach (function(Item,index,arr) {5Console.log (' The value of the current key is: ' +item+ ', the current corresponding index value is: ' +index+ ', the original array is: ' +arr)6 })7 //The value of the current item is: 1, the index value corresponding to the current item is: 0, the original array is: 1,2,3,48 //The value of the current item is: 2, the index value corresponding to the current item is: 1, the original array is: 1,2,3,49 //The value of the current item is: 3, the index value corresponding to the current item is: 2, the original array is: 1,2,3,4Ten //The value of the current item is: 4, the index value corresponding to the current item is: 3, the original array is: 1,2,3,4
Javascript-about for in and foreach