批改状态:未批改
老师批语:
js 中使用for或者forEach遍历数组
for语法:for (初始化表达式1; 判断表达式2; 自增表达式3) {// 循环体4}let items = ['一', '二', '三', '四', '五'];for (let i = 0; i < items.length; i++) {console.log(items[i]); // 一 二 三 四 五}for in / for offor in是ES5标准,遍历key.for of是ES6标准,遍历value.let items = ['一', '二', '三', '四', '五'];for (let item in items) {console.log(item); // 0 1 2 3 4}for (let item of items) {console.log(item); // 一 二 三 四 五}foreach()foreach是数组对象的一个方法.方法为每个数组元素调用一次函数(回调函数)语法:Array<number>.forEach( callbackfn: (value: number, index: number, array: number[]) => void, thisArg?: any): voidlet items = ['一', '二', '三', '四', '五'];items.forEach((item, index) => console.log(index,item)); // 0,一 1,二 2,三 3,四 4,五PHP中for, foreach遍历数组for语法:语法for(初始值;条件;增量){//循环体}$items = [1, 2, 3, 4, 5];for ($i=0; $i < count($items); $i++) {echo $items[$i]; // 1 2 3 4 5}foreach语法://语法一foreach(数组 as 值){}//语法二foreach(数组 as 键=>值){}$items = [1, 2, 3, 4, 5];foreach ($items as $k => $v) {echo $k,$v;}
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号