简体   繁体   中英

Javascript Nested JSON Parsing

I have a json object that object contains main nodes and nested nodes. Each node have a property "type", i want to remove the nodes object which contains the property "type = doc".Please find below example json image format. I attached 2 files one image is the input format and other one is the output format.

var json =  {
"navigations": [
{
  "disp_order": "1",
  "menu_id": "25266",
  "nodes": [
    {
      "disp_order": "2",
      "menu_id": "18951",
      "type": "DOC"
    }
  ],
  "type": "MENU"
},
{
  "disp_order": "20",
  "menu_id": "25204",
  "nodes": [
    {
      "disp_order": "1",
      "menu_id": "10295",
      "type": "DOC"
    },
    {
      "disp_order": "10",
      "menu_id": "25207",
      "nodes": [
        {
          "disp_order": "999",
          "menu_id": "17250",
          "type": "DOC"
        },

      ],
      "type": "MENU"
    },
    {
      "disp_order": "20",
      "menu_id": "25209",
      "nodes": [
        {
          "disp_order": "999",
          "menu_id": "18881",
          "type": "DOC"
        },

      ],
      "type": "MENU"
    },
  ],
  "type": "MENU"
},

]
}

输入 Json 格式

输出 Json 格式

https://jsfiddle.net/1hoctvdp/

function deleteNonMenu(obj) {
  if (obj.type == "DOC") {
    return true;
  }
  if (obj.nodes) {
    for (var i = 0; i < obj.nodes.length; i++) {
      var res = deleteNonMenu(obj.nodes[i]);
      if (res == true) {
        delete obj.nodes[i];
      }
    }
  }
  return false;
}

for (var i = 0; i < json.navigations.length; i++) {
  var result = deleteNonMenu(json.navigations[i]);
  if (result == true) {
    delete json.navigations[i];
  }
}
console.log(json);

Just as an alternative, you could use this ES6 function, which leaves the original object immutable, and creates the filtered version as return value:

 function withoutDocNodes(obj) { return Object(obj) !== obj ? obj // Primitive value: return w/o change : Object.assign(Array.isArray(obj) ? [] : {}, // Create array or object // With following properties: ...Object.keys(obj) // For each property: // Exclude those with DOC type in their value .filter( key => Object(obj[key]).type !== 'DOC') // Get recursive value (also without DOC types) .map( key => [withoutDocNodes(obj[key]), key] ) // Exclude if resulting value is an empty (object or array) .filter ( ([o]) => Object.keys(o).length ) // Produce key/value to add: arrays get index as property .map( ([o, key], i) => ({ [Array.isArray(obj) ? i : key]: o }) ) ); } const obj = { "navigations": [ { "disp_order": "1", "menu_id": "25266", "nodes": [ { "disp_order": "2", "menu_id": "18951", "type": "DOC" } ], "type": "MENU" }, { "disp_order": "20", "menu_id": "25204", "nodes": [ { "disp_order": "1", "menu_id": "10295", "type": "DOC" }, { "disp_order": "10", "menu_id": "25207", "nodes": [ { "disp_order": "999", "menu_id": "17250", "type": "DOC" }, ], "type": "MENU" }, { "disp_order": "20", "menu_id": "25209", "nodes": [ { "disp_order": "999", "menu_id": "18881", "type": "DOC" }, ], "type": "MENU" }, ], "type": "MENU" }, ] }; const result = withoutDocNodes(obj); console.log(result);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

This one is also working, try this code :

function mainFunction() {
  var data = new Array();
  data = excludeDocs(json.navigations);
}

function excludeDocs(nodes) { 
    var _docs = new Array(); 
    $.each(nodes, function(index, node) { 
      if(typeof node === 'object') { 
          if(node.type === 'DOC') {
              _docs.push(node.menu_id); 
           } 
           else if(typeof node.nodes === 'object') { 
               var _nodes = excludeDocs(node.nodes); 
                if(!(typeof nodes === 'object' && nodes.length > 0)) { 
                     delete node.nodes; 
                } 
                else { 
                   node.nodes = _nodes; 
                } 
            } 
      } 
  }); 
  return nodes.filter(function(n) { 
      return !_docs.includes(n.menu_id); 
  }); 
}

Here is a solution using object-scan . It's powerful for data processing once you wrap your head around it.

Note: (1) Expects well behaved input and (2) modifies the input in place

 // const objectScan = require('object-scan'); const prune = (type, input) => { objectScan(['**.nodes[*].type'], { filterFn: ({ value, gparent, gproperty }) => { if (value === type) { gparent.splice(gproperty, 1); } } })(input); }; const json = { navigations: [{ disp_order: '1', menu_id: '25266', nodes: [{ disp_order: '2', menu_id: '18951', type: 'DOC' }], type: 'MENU' }, { disp_order: '20', menu_id: '25204', nodes: [{ disp_order: '1', menu_id: '10295', type: 'DOC' }, { disp_order: '10', menu_id: '25207', nodes: [{ disp_order: '999', menu_id: '17250', type: 'DOC' }], type: 'MENU' }, { disp_order: '20', menu_id: '25209', nodes: [{ disp_order: '999', menu_id: '18881', type: 'DOC' }], type: 'MENU' }], type: 'MENU' }] }; prune('DOC', json); console.log(json); // => { navigations: [ { disp_order: '1', menu_id: '25266', nodes: [], type: 'MENU' }, { disp_order: '20', menu_id: '25204', nodes: [ { disp_order: '10', menu_id: '25207', nodes: [], type: 'MENU' }, { disp_order: '20', menu_id: '25209', nodes: [], type: 'MENU' } ], type: 'MENU' } ] }
 .as-console-wrapper {max-height: 100% !important; top: 0}
 <script src="https://bundle.run/object-scan@16.0.0"></script>

Disclaimer : I'm the author of object-scan

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM