10 examples of 'js foreach object' in JavaScript

Every line of 'js foreach object' 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
276export function objectForEach(
277 obj: { [key: string]: V },
278 cb: (value: V, key: string) => void
279): void {
280 Object.keys(obj).forEach(key => cb(obj[key], key));
281}
25function forEach(object, callback, context) {
26 var index, length;
27 if (object.length) {
28 if (Array.prototype.forEach) {
29 Array.prototype.forEach.call(object, callback, context);
30 } else {
31 for (index = 0, length = object.length; index < length; index += 1) {
32 callback.call(context || this, object[index], index, object);
33 }
34 }
35 } else {
36 for (index in object) {
37 if (object.hasOwnProperty(index)) {
38 callback.call(context || this, object[index], index, object);
39 }
40 }
41 }
42}
1026export function foreach(obj: { [key: string]: V }, action: (key: string, value: V) => void) {
1027
1028 for (const name in obj) {
1029 if (obj.hasOwnProperty == null || obj.hasOwnProperty(name)) {
1030 action(name, obj[name]);
1031 }
1032 }
1033}
57function forEach(o, fn) {
58 if (Array.isArray(o)) {
59 return o.forEach(fn);
60 }
61 else {
62 for (var key in o) {
63 if (o.hasOwnProperty(key)) {
64 fn(o[key], key)
65 }
66 }
67 }
68}
4export function forEach(obj, callback) {
5 if (obj) {
6 Object.keys(obj).forEach((key) => {
7 // eslint-disable-line no-restricted-syntax
8 if ({}.hasOwnProperty.call(obj, key)) {
9 callback(key, obj[key]);
10 }
11 });
12 }
13}
53export function forEachObj(obj, fn) {
54 for (const key in obj) {
55 if (Object.prototype.hasOwnProperty.call(obj, key)) {
56 fn(obj[key], key, obj)
57 }
58 }
59}
76export function forEach(
77 object: any, fn: (v: any, k?: string | number, c?: any, rc?: any) => any,
78 recurse: boolean | string = false, rootObject: any = object
79): void {
80 if (isEmpty(object)) { return; }
81 if ((isObject(object) || isArray(object)) && typeof fn === 'function') {
82 for (let key of Object.keys(object)) {
83 const value = object[key];
84 if (recurse === 'bottom-up' && (isObject(value) || isArray(value))) {
85 forEach(value, fn, recurse, rootObject);
86 }
87 fn(value, key, object, rootObject);
88 if (recurse === 'top-down' && (isObject(value) || isArray(value))) {
89 forEach(value, fn, recurse, rootObject);
90 }
91 }
92 } else if (typeof fn !== 'function') {
93 console.error('forEach error: Iterator must be a function.');
94 console.error(fn);
95 } else {
96 console.error('forEach error: Input object must be an object or array.');
97 console.error(object);
98 }
99}
48function _do(f) {
49 var i, values;
50 for(; a
34public forEach( cb : ( element : V, key : string, map : ObjectMap ) => any, thisArg : any ) {
35 for( var i in this._map ) {
36 if( this._map.hasOwnProperty( i ) ) {
37 cb.call( thisArg, this._map[i], i, this );
38 }
39 }
40}
10module.exports = function forEach(obj, fn) {
11 if (!isFunction(fn)) {
12 throw new TypeError('iterator must be a function');
13 }
14 var i, k,
15 isString = typeof obj === 'string',
16 l = obj.length,
17 context = arguments.length > 2 ? arguments[2] : null;
18 if (l === +l) {
19 for (i = 0; i < l; i++) {
20 if (context === null) {
21 fn(isString ? obj.charAt(i) : obj[i], i, obj);
22 } else {
23 fn.call(context, isString ? obj.charAt(i) : obj[i], i, obj);
24 }
25 }
26 } else {
27 for (k in obj) {
28 if (hasOwn.call(obj, k)) {
29 if (context === null) {
30 fn(obj[k], k, obj);
31 } else {
32 fn.call(context, obj[k], k, obj);
33 }
34 }
35 }
36 }
37};

Related snippets