Array map() 方法
定義和用法
map() 方法返回一個新數組,數組中的元素為原始數組元素調用函數處理后的值。
map() 方法按照原始數組元素順序依次處理元素。
注意: map() 不會對空數組進行檢測。
注意: map() 不會改變原始數組。
array.map(function(currentValue,index,arr), thisValue)
| 參數 | 描述 | ||||||||
|---|---|---|---|---|---|---|---|---|---|
| function(currentValue, index,arr) | 必須。函數,數組中的每個元素都會執行這個函數 函數參數:
|
||||||||
| thisValue | 可選。對象作為該執行回調時使用,傳遞給函數,用作 "this" 的值。 如果省略了 thisValue,或者傳入 null、undefined,那么回調函數的 this 為全局對象。 |
var numbers = [4, 9, 16, 25];
function myFunction() {
x = document.getElementById("demo")
x.innerHTML = numbers.map(Math.sqrt);
}
// 輸出 2,3,4,5
arr.map((item, index) => {
item.active = false
return item
})
