10 examples of 'es5 foreach' in JavaScript

Every line of 'es5 foreach' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
61export function forEach<T>(
62 arr: undefined | null | T[],
63 iterator: (value: T, index: number, array: T[]) => void,
64 thisArg?: any
65): void {
66 arr && proto.forEach.call(arr, iterator, thisArg)
67}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

70export function forEach<T>(
71 arr: undefined | null | T[],
72 iterator: (value: T, index: number, array: T[]) => void,
73 thisArg?: any,
74): void {
75 arr && proto.forEach.call(arr, iterator, thisArg)
76}
85forEach: function forEach(callbackfn /*, that = undefined */){
86 var f = ctx(callbackfn, arguments.length > 1 ? arguments[1] : undefined, 3)
87 , entry;
88 while(entry = entry ? entry.n : this._f){
89 f(entry.v, entry.k, this);
90 // revert to the last existing entry
91 while(entry && entry.r)entry = entry.p;
92 }
93},
53var forEach = function forEach(list, iteratee) {
54 var record = arguments[2] === undefined ? { index: 0 } : arguments[2];
55
56 for (var i = 0, len = list.length; i < len; i += 1) {
57 var item = list[i];
58 if (isArr(item)) {
59 forEach(item, iteratee, record);
60 } else if (!isUndefined(item)) {
61 iteratee(item, record.index);
62 record.index += 1;
63 }
64 }
65};
13function forEachAsync(arr, fn, thisArg) {
14 var result = PromiseA.resolve()
15 ;
16
17 arr.forEach(function (item, k) {
18 result = result.then(function () {
19
20 var ret
21 ;
22
23 if (thisArg) {
24 ret = fn.call(thisArg, item, k, arr);
25 } else {
26 ret = result = fn(item, k, arr);
27 }
28
29 if (!ret || !ret.then) {
30 ret = PromiseA.resolve(ret);
31 }
32
33 return ret.then(function (val) {
34 if (val === forEachAsync.__BREAK) {
35 return PromiseA.reject(new Error('break'));
36 //throw new Error('break');
37 }
38
39 return val;
40 });
41 });
42 });
43
44 result.catch(function (e) {
45 if ('break' !== e.message) {
46 throw e;
47 }
48 });
49
50 return result;
51}
65helper.forEachSeries = function forEachSeries (arr, iterator, callback) {
66 if (!arr.length) { return callback(); }
67 var completed = 0;
68 var iterate = function () {
69 iterator(arr[completed], function (err) {
70 if (err) {
71 callback(err);
72 callback = function () {};
73 } else {
74 completed += 1;
75 if (completed === arr.length) {
76 callback(null);
77 } else {
78 iterate();
79 }
80 }
81 });
82 };
83 iterate();
84};
14URLSearchParamsProto.forEach = function forEach(callback, thisArg) {
15 var names = Object.create(null);
16 this.toString()
17 .replace(/=[\s\S]*?(?:&|$)/g, '=')
18 .split('=')
19 .forEach(function (name) {
20 if (!name.length || name in names) return;
21 (names[name] = this.getAll(name)).forEach(function(value) {
22 callback.call(thisArg, value, name, this);
23 }, this);
24 }, this);
25};
6function forEach(arr, callback, thisObj) {
7 if (arr == null) {
8 return;
9 }
10 var i = -1,
11 n = arr.length;
12 while (++i < n) {
13 // we iterate over sparse items since there is no way to make it
14 // work properly on IE 7-8. see #64
15 if ( callback.call(thisObj, arr[i], i, arr) === false ) {
16 break;
17 }
18 }
19}
389function forEach (array, callback, thisObj)
390 {
391 for (var i=0; i<array.length; i++)
392 {
393 if (array.hasOwnProperty(i))
394 {
395 callback.call(thisObj, array[i], i, array);
396 }
397 }
398}
10exportTypedArrayMethod('forEach', function forEach(callbackfn /* , thisArg */) {
11 $forEach(aTypedArray(this), callbackfn, arguments.length > 1 ? arguments[1] : undefined);
12});

Related snippets