2635. Apply Transform Over Each Element in Array

更新於 發佈於 閱讀時間約 3 分鐘

今天要來嘗試的是,如何不用好用的Array.map 方法,來實現 Array.map 的功能。

What is Array.map

map() 方法會建立一個新的陣列,其內容為原陣列的每一個元素經由回呼函式運算後所回傳的結果之集合。

簡單來說就是把陣列內的每一個值,個別跑函式,把新的值回傳出成新的陣列,並不會取代原本的陣列。舉例來說:

let A = [1, 2, 3]

let B = A.map( function(n) {
return n*2
})

console.log(B) // [ 2, 4, 6 ]

(類似的 .forEach() 則是修改原有的陣列,並且不會輸出新的值)

題目

Given an integer array arr and a mapping function fn, return a new array with a transformation applied to each element.

The returned array should be created such that returnedArray[i] = fn(arr[i], i).

Please solve it without the built-in Array.map method.

 

Example 1:

Input: arr = [1,2,3], fn = function plusone(n) { return n + 1; }
Output: [2,3,4]
Explanation:
const newArray = map(arr, plusone); // [2,3,4]
The function increases each value in the array by one.

Example 2:

Input: arr = [1,2,3], fn = function plusI(n, i) { return n + i; }
Output: [1,3,5]
Explanation: The function increases each value by the index it resides in.

Example 3:

Input: arr = [10,20,30], fn = function constant() { return 42; }
Output: [42,42,42]
Explanation: The function always returns 42.

 

解題思路:

首先我們要設計一個函式 map,讓可以接受題目的 arr 跟 fn

function map(arr, fn){
}

因為題目要我們回傳一個新的陣列,所以在 function 內部要先創建一個空的陣列 returnedArray 來儲存結果
已經做過很多練習的我們,可以很快寫出如何在陣列的數量底下,重複跑程式的迴圈

function map(arr, fn) {
let returnedArray=[]
for (let i = 0; i < arr.length; i++) {}

接著,我們該如何讓 arr 跑過 fn 函式並添加到 returnedArray ,我們可以直接照著題目的描述:

function map(arr, fn) {
let returnedArray = [];
for (let i = 0; i < arr.length; i++) {
returnedArray[i] = (fn(arr[i], i));
}
return returnedArray;
}

完成!


留言
avatar-img
留言分享你的想法!
avatar-img
高的沙龍
0會員
5內容數
高的沙龍的其他內容
2024/03/16
(略),array 是用來將陣列的值進行累加,我們來看看怎麼怎麼達成吧: Given an integer array nums, a reducer function fn, and an initial value init, return the final result obtained
2024/03/16
(略),array 是用來將陣列的值進行累加,我們來看看怎麼怎麼達成吧: Given an integer array nums, a reducer function fn, and an initial value init, return the final result obtained
看更多
你可能也想看
Thumbnail
孩子寫功課時瞇眼?小心近視!這款喜光全光譜TIONE⁺光健康智慧檯燈,獲眼科院長推薦,網路好評不斷!全光譜LED、180cm大照明範圍、5段亮度及色溫調整、350度萬向旋轉,讓孩子學習更舒適、保護眼睛!
Thumbnail
孩子寫功課時瞇眼?小心近視!這款喜光全光譜TIONE⁺光健康智慧檯燈,獲眼科院長推薦,網路好評不斷!全光譜LED、180cm大照明範圍、5段亮度及色溫調整、350度萬向旋轉,讓孩子學習更舒適、保護眼睛!
Thumbnail
創作者營運專員/經理(Operations Specialist/Manager)將負責對平台成長及收入至關重要的 Partnership 夥伴創作者開發及營運。你將發揮對知識與內容變現、影響力變現的精準判斷力,找到你心中的潛力新星或有聲量的中大型創作者加入 vocus。
Thumbnail
創作者營運專員/經理(Operations Specialist/Manager)將負責對平台成長及收入至關重要的 Partnership 夥伴創作者開發及營運。你將發揮對知識與內容變現、影響力變現的精準判斷力,找到你心中的潛力新星或有聲量的中大型創作者加入 vocus。
Thumbnail
近年來,折疊手機市場風起雲湧,三星、華為等品牌紛紛推出創新產品, 而作為科技巨頭的 Apple 卻始終按兵不動。然而,隨著多項專利曝光和供應鏈消息的傳出, 關於 Apple 首款摺疊機的傳聞越來越多,讓全球果粉們翹首以待。 究竟 Apple 摺疊機是否真的即將問世?它會帶來哪些創新設計與功能?
Thumbnail
近年來,折疊手機市場風起雲湧,三星、華為等品牌紛紛推出創新產品, 而作為科技巨頭的 Apple 卻始終按兵不動。然而,隨著多項專利曝光和供應鏈消息的傳出, 關於 Apple 首款摺疊機的傳聞越來越多,讓全球果粉們翹首以待。 究竟 Apple 摺疊機是否真的即將問世?它會帶來哪些創新設計與功能?
Thumbnail
這篇內容,將會講解什麼是陣列,以及與陣列相關的知識。包括陣列的簡介、陣列的資料限制、陣列的維度、一維陣列、二維陣列。
Thumbnail
這篇內容,將會講解什麼是陣列,以及與陣列相關的知識。包括陣列的簡介、陣列的資料限制、陣列的維度、一維陣列、二維陣列。
Thumbnail
題目敘述 Make Two Arrays Equal by Reversing Subarrays 題目給定兩個輸入陣列,請問能否透過子陣列的反轉讓兩個陣列相等? 子陣列的反轉操作次數不受限制。 如果可以,返回True 如果不行,返回False
Thumbnail
題目敘述 Make Two Arrays Equal by Reversing Subarrays 題目給定兩個輸入陣列,請問能否透過子陣列的反轉讓兩個陣列相等? 子陣列的反轉操作次數不受限制。 如果可以,返回True 如果不行,返回False
Thumbnail
【這個系列,目標是以比較輕鬆的方式讓大家一起學習AE表達式。】 本文是番外篇 3,主要是一些概念的補充,介紹陣列。
Thumbnail
【這個系列,目標是以比較輕鬆的方式讓大家一起學習AE表達式。】 本文是番外篇 3,主要是一些概念的補充,介紹陣列。
Thumbnail
This August, I will go to Aarhus University in Denmark to study for a master's degree. Introduction of the master's degree in agricultural biology Wh
Thumbnail
This August, I will go to Aarhus University in Denmark to study for a master's degree. Introduction of the master's degree in agricultural biology Wh
Thumbnail
Mastering the Game: How to Apply Game Theory to Succeed in the Real World Game theory, a branch of mathematics concerned with the analysis of strateg
Thumbnail
Mastering the Game: How to Apply Game Theory to Succeed in the Real World Game theory, a branch of mathematics concerned with the analysis of strateg
追蹤感興趣的內容從 Google News 追蹤更多 vocus 的最新精選內容追蹤 Google News