各語言 Foreach 寫法

在寫演算法的時候,常常會用到Foreach,由於自己常常在各語言中切換使用,這邊整理一下常用的Foreach寫法

❌ 不易閱讀的寫法

最基本的寫法,但也非常難閱讀

1
2
3
4
arr = [4, 5, 6]
for( let i = 0 ; i < arr.length ; i++ ){
console.log(i, arr[i]);
}

✅ 更好的寫法

各語言有不同用法,這邊列了幾個我常用的語言

Javascript

forEach

1
2
3
4
5
arr = [4, 5, 6]
arr.forEach((val, key) => console.log(key, val))

obj = {a:1, b:2, c:3}
Object.keys(obj).forEach((key) => console.log(key, objs[key]))

for...of

1
2
3
4
5
6
7
8
9
arr = [4, 5, 6]
for (const [key, val] of arr.entries()) {
console.log(key, val)
}

obj = {a:1, b:2}
for (const [key, val] of Object.entries(obj)) {
console.log(key, val)
}

PHP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
$arr = [4, 5, 6];
foreach($arr as $val) {
dump("$val");
}
foreach($arr as $key => $val) {
dump("$key, $val");
}

$obj = ['a' => 1, 'b' => 2, 'c' => 3];
foreach($obj as $val) {
dump("$val");
}
foreach($obj as $key => $val) {
dump("$key, $val");
}

Python

1
2
3
4
5
6
7
8
9
10
11
12
13
# array
arr = [4, 5, 6]
for val in arr:
print(val)
for key, val in enumerate(arr):
print(key, val)

# dict
dicts = {'a': 1, 'b': 2, 'c': 3}
for key in dicts:
print(key, dicts[key])
for key, val in dicts.items():
print(key, val)

參考