jmeta.js
// It keeps track of its depth and running path and passes it to the supplied callback func
It keeps track of its depth and running path and passes it to the supplied callback func
It keeps track of its depth and running path and passes it to the supplied callback func
function walkJSON (obj, func) {
  const traverse = (obj, curDepth, path, key) => {
    if (curDepth && key) func.apply(this, [key, curDepth, path])
    if (!obj) return 
    if (_.isArray(obj)) {
      obj.forEach((val, index) => {
        if (_.isObject(val) || _.isArray(val)) {
          let newPath = path.length ? `${path}[${index}]` : `[${index}]`
          traverse(val, curDepth + 1, newPath)
        }
      })
    } else if (_.isObject(obj)) {
      Object.keys(obj).forEach(key => {
        if (obj[key] !== null) {
          let newPath = path.length ? `${path}.${key}` : key
          traverse(obj[key], curDepth + 1, newPath, key)
        } else {
          let newPath = path.length ? `${path}.${key}` : key
          func.apply(this, [key, curDepth + 1, newPath])
        }
      })
    }
  }
  return traverse(obj, 0, '')
}
function walkJSON ( obj , func ) { const traverse = ( obj , curDepth , path , key ) => { if ( curDepth && key ) func . apply ( this , [ key , curDepth , path ] ) if ( ! obj ) return if ( _ . isArray ( obj ) ) { obj . forEach ( ( val , index ) => { if ( _ . isObject ( val ) || _ . isArray ( val ) ) { let newPath = path . length ? ` ${ path } ${ index } ` : ` ${ index } ` traverse ( val , curDepth + 1 , newPath ) } } ) } else if ( _ . isObject ( obj ) ) { Object . keys ( obj ) . forEach ( key => { if ( obj [ key ] !== null ) { let newPath = path . length ? ` ${ path } ${ key } ` : key traverse ( obj [ key ] , curDepth + 1 , newPath , key ) } else { let newPath = path . length ? ` ${ path } ${ key } ` : key func . apply ( this , [ key , curDepth + 1 , newPath ] ) } } ) } } return traverse ( obj , 0 , '' ) }
It keeps track of its depth and running path and passes it to the supplied callback func
It keeps track of its depth and running path and passes it to the supplied callback func
// Bail on falsey obj ref // If this entry is an array then loop over its contents // If this entry is an object then loop over its keys