|
1 | | -# [2804. Array Prototype ForEach](https://leetcode.cn/problems/array-prototype-foreach) |
| 1 | +# [2804. 数组原型的 forEach 方法](https://leetcode.cn/problems/array-prototype-foreach) |
2 | 2 |
|
3 | 3 | [English Version](/solution/2800-2899/2804.Array%20Prototype%20ForEach/README_EN.md) |
4 | 4 |
|
5 | 5 | ## 题目描述 |
6 | 6 |
|
7 | 7 | <!-- 这里写题目描述 --> |
8 | 8 |
|
9 | | -<p>Write your version of method <code>forEach</code> that enhances all arrays such that you can call the <code>array.forEach(callback, context)</code> method on any array and it will execute <code>callback</code> on each element of the array. Method <code>forEach</code> should not return anything.</p> |
| 9 | +<p>编写一个数组方法 <code>forEach</code>,使其可以在任何数组上调用 <code>array.forEach(callback, context)</code> 方法,它将在数组的每个元素上执行回调函数。<code>forEach</code> 方法不应该返回任何内容。</p> |
10 | 10 |
|
11 | | -<p><code>callback</code> accepts the following arguments:</p> |
| 11 | +<p>回调函数 <code>callback</code> 接受以下参数:</p> |
12 | 12 |
|
13 | 13 | <ul> |
14 | | - <li><code>value</code> - represents the current element being processed in the array. It is the value of the element in the current iteration.</li> |
15 | | - <li><code>index</code> - represents the index of the current element being processed in the array.</li> |
16 | | - <li><code>array</code> - represents the array itself, allowing access to the entire array within the callback function.</li> |
| 14 | + <li><code>value</code> - 表示数组中当前正在处理的元素的值。</li> |
| 15 | + <li><code>index</code> - 表示数组中当前正在处理的元素的索引。</li> |
| 16 | + <li><code>array</code> - 表示数组本身,在回调函数内部可以访问整个数组。</li> |
17 | 17 | </ul> |
18 | 18 |
|
19 | | -<p>The <code>context</code> is the object that should be passed as the function context parameter to the <code>callback</code> function, ensuring that the <code>this</code> keyword within the <code>callback</code> function refers to this <code>context</code> object.</p> |
| 19 | +<p>上下文 <code>context</code> 应该是作为函数上下文参数传递给回调函数的对象,确保回调函数内部的 <code>this</code> 关键字引用此上下文对象。</p> |
20 | 20 |
|
21 | | -<p>Try to implement it without using the built-in array methods.</p> |
| 21 | +<p>尝试在不使用内置数组方法的情况下实现这个方法。</p> |
22 | 22 |
|
23 | 23 | <p> </p> |
24 | | -<p><strong class="example">Example 1:</strong></p> |
| 24 | + |
| 25 | +<p><b>示例 1:</b></p> |
25 | 26 |
|
26 | 27 | <pre> |
27 | | -<strong>Input:</strong> |
| 28 | +<b>输入:</b> |
28 | 29 | arr = [1,2,3], |
29 | 30 | callback = (val, i, arr) => arr[i] = val * 2, |
30 | | -context = {"context":true} |
31 | | -<strong>Output:</strong> [2,4,6] |
32 | | -<strong>Explanation:</strong> |
| 31 | +context = {"context":true} |
| 32 | +<b>输出:</b>[2,4,6] |
| 33 | +<b>解释:</b> |
33 | 34 | arr.forEach(callback, context) |
34 | 35 | console.log(arr) // [2,4,6] |
35 | 36 |
|
36 | | -The callback is executed on each element of the array. |
| 37 | +回调函数在数组的每个元素上执行。 |
37 | 38 | </pre> |
38 | 39 |
|
39 | | -<p><strong class="example">Example 2:</strong></p> |
| 40 | +<p><strong class="example">示例 2:</strong></p> |
40 | 41 |
|
41 | 42 | <pre> |
42 | | -<strong>Input:</strong> |
| 43 | +<b>输入:</b> |
43 | 44 | arr = [true, true, false, false], |
44 | 45 | callback = (val, i, arr) => arr[i] = this, |
45 | | -context = {"context": false} |
46 | | -<strong>Output:</strong> [{"context":false},{"context":false},{"context":false},{"context":false}] |
47 | | -<strong>Explanation:</strong> |
| 46 | +context = {"context": false} |
| 47 | +<b>输出:</b>[{"context":false},{"context":false},{"context":false},{"context":false}] |
| 48 | +<b>解释:</b> |
48 | 49 | arr.forEach(callback, context) |
49 | | -console.log(arr) // [{"context":false},{"context":false},{"context":false},{"context":false}] |
| 50 | +console.log(arr) // [{"context":false},{"context":false},{"context":false},{"context":false}] |
50 | 51 |
|
51 | | -The callback is executed on each element of the array with the right context. |
| 52 | +回调函数在数组的每个元素上以正确的上下文执行。 |
52 | 53 | </pre> |
53 | 54 |
|
54 | | -<p><strong class="example">Example 3:</strong></p> |
| 55 | +<p><strong class="example">示例 3:</strong></p> |
55 | 56 |
|
56 | 57 | <pre> |
57 | | -<strong>Input:</strong> |
| 58 | +<b>输入:</b> |
58 | 59 | arr = [true, true, false, false], |
59 | 60 | callback = (val, i, arr) => arr[i] = !val, |
60 | | -context = {"context": 5} |
61 | | -<strong>Output:</strong> [false,false,true,true] |
| 61 | +context = {"context": 5} |
| 62 | +<b>输出:</b>[false,false,true,true] |
62 | 63 | </pre> |
63 | 64 |
|
64 | 65 | <p> </p> |
65 | | -<p><strong>Constraints:</strong></p> |
| 66 | + |
| 67 | +<p><strong>提示:</strong></p> |
66 | 68 |
|
67 | 69 | <ul> |
68 | | - <li><code>arr</code> is a valid JSON array</li> |
69 | | - <li><code>context</code> is a valid JSON object</li> |
70 | | - <li><code>fn</code> is a function</li> |
| 70 | + <li><code>arr</code> 是一个有效的 JSON 数组</li> |
| 71 | + <li><code>context</code> 是一个有效的 JSON 对象</li> |
| 72 | + <li><code>fn</code> 是一个函数</li> |
71 | 73 | <li><code>0 <= arr.length <= 10<sup>5</sup></code></li> |
72 | 74 | </ul> |
73 | 75 |
|
|
0 commit comments