3 examples of 'javascript iterate json object' in JavaScript

Every line of 'javascript iterate json 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
this disclaimer
365(function iterate(obj) {
366
367 let keys = Object.keys(obj);
368 for (let i = 0, l = keys.length; i < l; i++) {
369 let objProp = obj[keys[i]];
370 if (objProp instanceof Object) iterate(objProp);
371 }
372
373 // if there are any existing target objects (objects that we're already observing)...
374 //let c = targets.indexOf(obj);
375 let c = -1;
376 for (let i = 0, l = targets.length; i < l; i++) {
377 if (obj === targets[i]) {
378 c = i;
379 break;
380 }
381 }
382 if (c > -1) {
383
384 // ...then we want to determine if the observables for that object match our current observable
385 let currentTargetProxy = targetsProxy[c];
386 let d = currentTargetProxy.length;
387
388 while (d--) {
389 // if we do have an observable monitoring the object thats about to be overwritten
390 // then we can remove that observable from the target object
391 if (observable === currentTargetProxy[d].observable) {
392 currentTargetProxy.splice(d, 1);
393 break;
394 }
395 }
396
397 // if there are no more observables assigned to the target object, then we can remove
398 // the target object altogether. this is necessary to prevent growing memory consumption particularly with large data sets
399 if (currentTargetProxy.length === 0) {
400 targets[c] = null;
401 //targetsProxy.splice(c, 1);
402 //targets.splice(c, 1);
403 }
404 }
405
406})(targetProp)
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

3681function iterateObj(name, keys, obj, options) {
3682 var parts = [];
3683 each(keys, function(key) {
3684 var val;
3685 var descriptor = Object.getOwnPropertyDescriptor(obj, key);
3686 var hasGetter = descriptor && descriptor.get;
3687 var hasSetter = descriptor && descriptor.set;
3688
3689 if (!options.accessGetter && hasGetter) {
3690 val = '(...)';
3691 } else {
3692 try {
3693 val = obj[key];
3694
3695 if (contain(options.ignore, val)) {
3696 return;
3697 }
3698
3699 if (isPromise(val)) {
3700 val.catch(function() {});
3701 }
3702 } catch (e) {
3703 val = e.message;
3704 }
3705 }
3706
3707 parts.push(''.concat(wrapKey(key), ':').concat(exports(val, options)));
3708
3709 if (hasGetter) {
3710 parts.push(
3711 ''
3712 .concat(wrapKey('get ' + toStr(key)), ':')
3713 .concat(exports(descriptor.get, options))
3714 );
3715 }
3716
3717 if (hasSetter) {
3718 parts.push(
3719 ''
3720 .concat(wrapKey('set ' + toStr(key)), ':')
3721 .concat(exports(descriptor.set, options))
3722 );
3723 }
3724 });
3725 return '"'.concat(name, '":{') + parts.join(',') + '}';
3726}
84function iterate(obj, cb, depth = 0, toplevel = null) {
85 var ret = [];
86 var pad = ""
87 for (var i = 0;i < depth; i++) {
88 pad += " ";
89 }
90 if (toplevel == null)
91 toplevel = obj;
92 for ([name, value] of Object.entries(obj)) {
93 if (typeof value == "object" && !Array.isArray(value)) {
94 if (value['content'] != null)
95 ret.push(cb(name, value['content'], toplevel));
96 ret = ret.concat(iterate(value, cb, depth + 4, toplevel));
97 }
98 }
99 return ret;
100}

Related snippets