for-ofとforEachとmapの使い分け
1. for-of
for (const v of ["a", "b", "c"]) {
if (v === 'x') {
break;
}
console.log(v);
}
参考
https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Statements/for...of
2. forEach()
["a", "b", "c"].forEach((v) => {
console.log(v);
});
参考
https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
3. map()
var nums = [1, 2, 3].map((v) => {
return v * 10;
});
console.log(nums);
参考
補足: Map/Setなどのループ
var set = new Set([1, 2, 3]);
var map = new Map([
["a", 1],
["b", 2],
["c", 3],
]);
for (var v of set) {
console.log(v);
}
map.forEach((v, k) => {
console.log(k, v);
});
参考
https://stackoverflow.com/questions/50844095/should-one-use-for-of-or-foreach-when-iterating-through-an-array