毎回for文書くたびに調べてるので
配列のforeach
phpだとこんな感じのやつ
$array = [1, 2, 3];
foreach($array as $a){
echo $a;
}
// 1
// 2
// 3
jsだとこう
const array = [1, 2, 3];
for(let a of array){
console.log(a);
}
// 1
// 2
// 3
連想配列のforeach
phpだとこんな感じのやつ
$array_assoc = [
1 => 'one',
2 => 'two',
3 => 'three'
];
foreach($array_assoc as $k => $v){
echo $k . ' is ' . $v . "\n";
}
// 1 is one
// 2 is two
// 3 is three
jsだとこう
const array_assoc = {
1 : 'one',
2 : 'two',
3 : 'three'
};
for(let [k, v] of Object.entries(array_assoc)){
console.log(k + ' is ' + v + "\n");
}
// 1 is one
// 2 is two
// 3 is three
※まあ、jsのObjectは正確には連想配列ではないけど