Skip to content

2804. Array Prototype ForEach

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
type JSONValue =
  | null
  | boolean
  | number
  | string
  | JSONValue[]
  | { [key: string]: JSONValue };
type Callback = (
  currentValue: JSONValue,
  index: number,
  array: JSONValue[]
) => any;
type Context = Record<string, JSONValue>;

Array.prototype.forEach = function (
  callback: Callback,
  context: Context
): void {
  for (let i = 0; i < this.length; ++i) {
    callback.call(context, this[i], i, this);
  }
};