Description of the Foreach API:
Https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
Description
forEach
The scope of the traversal is callback
determined before the first call . forEach
items added to the array after the call are not callback
accessed . If the values that already exist are changed, the values passed to callback
them are the forEach
values that are traversed to their moment. Deleted items are not traversed to. If the accessed element is deleted at iteration (for example shift()
, used), then the element will be skipped
Example:
<!DOCTYPE HTML><HTMLLang= "zh"> <Head> <MetaCharSet= "UTF-8" /> <Metaname= "Viewport"content= "Width=device-width, initial-scale=1.0" /> <Metahttp-equiv= "X-ua-compatible"content= "Ie=edge" /> <title>JS foreach function considerations</title> </Head> <Body> <Scriptsrc= "Https://cdn.bootcss.com/lodash.js/4.17.10/lodash.min.js"></Script> <Scripttype= "Text/javascript"> varwords= [" One", " Both", "three", " Four"]; Words.foreach (function(Word) {console.log (word); if(Word=== " Both") { //will change the output order //Words.shift (); //does not change the output orderWords.push ('5') //cannot use break // Break //cannot use continue //Continue } }); </Script> </Body></HTML>
JS foreach function considerations (break, continue)