{ "data": { "question": { "questionId": "2747", "questionFrontendId": "2635", "categoryTitle": "JavaScript", "boundTopicId": 2219915, "title": "Apply Transform Over Each Element in Array", "titleSlug": "apply-transform-over-each-element-in-array", "content": "
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.
\n
Example 1:
\n\n\nInput: arr = [1,2,3], fn = function plusone(n) { return n + 1; }\nOutput: [2,3,4]\nExplanation:\nconst newArray = map(arr, plusone); // [2,3,4]\nThe function increases each value in the array by one. \n\n\n
Example 2:
\n\n\nInput: arr = [1,2,3], fn = function plusI(n, i) { return n + i; }\nOutput: [1,3,5]\nExplanation: The function increases each value by the index it resides in.\n\n\n
Example 3:
\n\n\nInput: arr = [10,20,30], fn = function constant() { return 42; }\nOutput: [42,42,42]\nExplanation: The function always returns 42.\n\n\n
\n
Constraints:
\n\n0 <= arr.length <= 1000
-109 <= arr[i] <= 109
fn
returns a number编写一个函数,这个函数接收一个整数数组 arr
和一个映射函数 fn
,通过该映射函数返回一个新的数组。
返回数组的创建语句应为 returnedArray[i] = fn(arr[i], i)
。
请你在不使用内置方法 Array.map
的前提下解决这个问题。
\n\n
示例 1:
\n\n\n输入:arr = [1,2,3], fn = function plusone(n) { return n + 1; }\n输出:[2,3,4]\n解释: \nconst newArray = map(arr, plusone); // [2,3,4]\n此映射函数返回值是将数组中每个元素的值加 1。\n\n\n
示例 2:
\n\n\n输入:arr = [1,2,3], fn = function plusI(n, i) { return n + i; }\n输出:[1,3,5]\n解释:此映射函数返回值根据输入数组索引增加每个值。\n\n\n
示例 3:
\n\n\n输入:arr = [10,20,30], fn = function constant() { return 42; }\n输出:[42,42,42]\n解释:此映射函数返回值恒为 42。\n\n\n
\n\n
提示:
\n\n0 <= arr.length <= 1000
-109 <= arr[i] <= 109
fn
返回一个数\\u7248\\u672c\\uff1a \\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"],\"typescript\":[\"TypeScript\",\" TypeScript 5.1.6<\\/p>\\r\\n\\r\\n Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2022<\\/p>\\r\\n\\r\\n lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"]}",
"book": null,
"isSubscribed": false,
"isDailyQuestion": false,
"dailyRecordStatus": null,
"editorType": "CKEDITOR",
"ugcQuestionId": null,
"style": "LEETCODE",
"exampleTestcases": "function plusone(n) { return n + 1; }\n[1,2,3]\nfunction plusI(n, i) { return n + i; }\n[1,2,3]\nfunction constant(n, i) { return 42; }\n[10,20,30]",
"__typename": "QuestionNode"
}
}
}Node.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n
--harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f \\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n