記事ディレクトリ
メソッド宣言
map()メソッドは、新しい配列を作成します。その結果、配列内の各要素は、提供された関数への1回の呼び出しの戻り値になります。
var new_array = arr.map(callback[, thisArg])
その中のコールバック:
function callback(currentValue[, index[, array]]) {
// Return element for new_array
}
forEach()メソッドは、配列の要素ごとに指定された関数を1回実行します。
arr.forEach(callback(currentValue [, index [, array]])[, thisArg])
デモ
var arr = [1, 2, 3, 4, 5]
// arr.map(...)会有一个新的数组返回
arr.map(function(element, index, array) {
return element * 2;
})
// (5) [2, 4, 6, 8, 10]
// arr.forEach() 只是对遍历的每个元素做一些操作(如输出)
arr.forEach(function(element, index, array) {
console.log(element * 2)
})
// 2
// 4
// 6
// 8
// 10