{ "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.

\n\n

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

\n\n

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

\n\n

 

\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\n\n", "translatedTitle": "转换数组中的每个元素", "translatedContent": "

编写一个函数,这个函数接收一个整数数组 arr 和一个映射函数  fn ,通过该映射函数返回一个新的数组。

\n\n

返回数组的创建语句应为 returnedArray[i] = fn(arr[i], i) 。

\n\n

请你在不使用内置方法 Array.map 的前提下解决这个问题。

\n\n

 

\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\n\n​​​​​​", "isPaidOnly": false, "difficulty": "Easy", "likes": 8, "dislikes": 0, "isLiked": null, "similarQuestions": "[]", "contributors": [], "langToValidPlayground": "{\"cpp\": true, \"java\": true, \"python\": true, \"python3\": true, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"typescript\": false, \"bash\": false, \"php\": false, \"swift\": false, \"kotlin\": false, \"dart\": false, \"golang\": false, \"ruby\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"rust\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"pythondata\": false, \"react\": false, \"vanillajs\": false, \"postgresql\": false}", "topicTags": [], "companyTagStats": null, "codeSnippets": [ { "lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[]} arr\n * @param {Function} fn\n * @return {number[]}\n */\nvar map = function(arr, fn) {\n \n};", "__typename": "CodeSnippetNode" }, { "lang": "TypeScript", "langSlug": "typescript", "code": "function map(arr: number[], fn: (n: number, i: number) => number): number[] {\n\t\n};", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"9.1K\", \"totalSubmission\": \"11.9K\", \"totalAcceptedRaw\": 9149, \"totalSubmissionRaw\": 11908, \"acRate\": \"76.8%\"}", "hints": [ "Start by creating an array that will eventually be returned.", "Loop over each element in the passed array. Push fn(arr[i]) to the returned array." ], "solution": null, "status": null, "sampleTestCase": "function plusone(n) { return n + 1; }\n[1,2,3]", "metaData": "{\n \"name\": \"map\",\n \"params\": [\n {\n \"name\": \"arr\",\n \"type\": \"integer[]\"\n },\n {\n \"type\": \"string\",\n \"name\": \"fn\"\n }\n ],\n \"return\": {\n \"type\": \"integer[]\"\n },\n \"manual\": true,\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ]\n}", "judgerAvailable": true, "judgeType": "large", "mysqlSchemas": [], "enableRunCode": true, "envInfo": "{\"javascript\":[\"JavaScript\",\"

\\u7248\\u672c\\uff1aNode.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n

\\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a --harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f \\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/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>\"],\"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" } } }