Object property traverse - Javascript Object

Javascript examples for Object:Object Property

Description

Object property traverse

Demo Code



var arr_tree = []
var query = ''

Object.prototype.traverse = function(obj_start) {
       /*from  ww  w  .j a v a2s.c  o  m*/
    function actionPerType(father, prop, val) {
        if(prop != null && val.type != 'object' && val.type != 'array') {
              arr_tree.push({ 
                  id: generateId(),
                  father,
                  prop, 
                  val,
                  path: null 
                })
            }
            
            if(val.type == 'object' || val.type == 'array') {
                    query += `[${prop}]`
                    iterate(val) 
            }
            else {
                query += `[${prop}]=${val}&\n`
            }

       }
       
      function iterate(obj) {
           var props = Object.keys(obj) 
           props.forEach(prop => {
             actionPerType(obj, prop, obj[prop])
           })
      }
     
      actionPerType(obj_start || this, null, obj_start || this)
}




input.traverse()

console.log(arr_tree)
console.log(query)

Related Tutorials