10 examples of 'js foreach key value' in JavaScript

Every line of 'js foreach key value' 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
29(function TestForEachNoValue() {
30 res = "c) ";
31 var myMap = new Map([['key1', ''], ['key2',], ['key3', undefined], ['key4', null]]);
32 myMap.forEach(logElement);
33
34 assertEquals("c) map[key1] = '' map[key2] = 'undefined' map[key3] = 'undefined' map[key4] = 'null' ", res);
35})();
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

133forEach(callbackfn: (value: V, index: K, map: this) => void, thisArg?: any): void {
134 for (const property of Object.keys(this._keys)) {
135 callbackfn.call(thisArg, this._values[property], this._keys[property], this);
136 }
137}
25function forEach(value, fn, path) {
26 path = path || '';
27
28 if (Array.isArray(value)) {
29 forEachArray(value, fn, path);
30 } else if (isPlainObject(value)) {
31 forEachObject(value, fn, path);
32 }
33}
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}
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}
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}
28function forEach(obj, fn) {
29 Object.keys(obj).forEach(function(name, index) {
30 fn(obj[name], name, index);
31 });
32}
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};
3function forEach(list, iterator, context) {
4 var keys = Object.keys(list)
5
6 if (arguments.length < 3) {
7 context = this
8 }
9
10 for (var i = 0, len = keys.length; i < len; i++) {
11 var key = keys[i]
12 , value = list[key]
13
14 iterator.call(context, value, key, list)
15 }
16}
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}

Related snippets