10 examples of 'foreach object js' in JavaScript

Every line of 'foreach object js' 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
276export function objectForEach<V>(
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}
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

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}
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}
1026export function foreach<V>(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}
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}
48function _do(f) {
49 var i, values;
50 for(; a<args_length; ++a) {
51 values = args[a];
52 for(i in values) if(values.hasOwnProperty(i)) f(values[i], i);
53 }
54}
34public forEach( cb : ( element : V, key : string, map : ObjectMap<V> ) => 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}
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}
226function forEach(obj, fn, basePath) {
227 basePath = basePath || []
228
229 let results = []
230 if (basePath.length > 0) {
231 const newResults = fn(obj, basePath[basePath.length - 1], basePath)
232 if (newResults) {
233 results = results.concat(newResults)
234 }
235 }
236
237 if (Array.isArray(obj)) {
238 const arrayResults = obj.map((val, key) => {
239 return forEach(val, fn, basePath.concat(key))
240 })
241 if (arrayResults) {
242 results = results.concat(arrayResults)
243 }
244 }
245 else if (isObject(obj)) {
246 const moreResults = Object.keys(obj).map((key) => {
247 return forEach(obj[key], fn, basePath.concat(key))
248 })
249 if (moreResults) {
250 results = results.concat(moreResults)
251 }
252 }
253
254 results = flatten(results)
255 return results
256}

Related snippets