Home > Article > Web Front-end > Introduction to the use of global function each in jquery_jquery
jquery contains two each, one is $().each and the other is $.each. The difference is that the former is a built-in function of the jquery object and the latter is a traversal function of the object. It is generally used to obtain different objects in ajax. Data traversal takes json as an example. jquery's getJSON shortcut method can conveniently obtain the json file in the specified URL. jquery will built-in call the js native eval function to parse the json text and convert it into a js object, and then traverse through the each global function to obtain it. The value syntax is each(data,[params],function(current item index, current item)). This is also a big difference from the built-in function each. The index in the built-in function is an identifier used to indicate the position from which Start traversing, followed by the element element to specify how many elements to traverse. The heavy index of each in the global function refers to the key in each key-value pair in json, which is KEY! The author in the jquery basic tutorial gave an example and wrote the corresponding The code however failed to correctly sample the parameters. The sample code is as follows
$.each(data,function(entryIndex,entry){ var html = entry['term']}); Friends who have read this book must be confused about what entryIndex is? In fact, entryIndex is just 'term' However, the author did not use index substitution but filled in the key values directly from json. Through debugging, I found that the following questions are generally translated as indexes. My personal opinion is that the entry should be called the key. It is an attribute value that contains key-value pairs. When a field is imported using the key as an index, the corresponding value is returned through a method similar to the get accessor. I hope it can provide answers to friends who have doubts.