简体   繁体   中英

How to get array of checked Values from the nested JSON Object

I have a nested json having the check key with boolean values. I want name key value only for those with check is true in a result array.

Sample JSON

 const treeMetaData = [
{
  name  :  'database',
  checked : true,
  schemas  : [
    {
      name  : "schema1",
      checked : true,
      tables : [
        {
          name  : "table1",
          checked : true,
          columns : [
            {
              name  : "column1",
              checked : false,
            }, 
            {
              name  : "column2",
              checked : true,
            }
          ]
        },
      ]
    },
    {
      name  : "schema2",
      checked : true,
      tables : [
        {
          name  : "table2",
          checked : true,
          columns : [
            {
              name  : "column4",
              checked : true,
            }, 
            {
              name  : "column5",
              checked : false,
            }
          ]
        },
      ]
    },

  ]
}]

The the output I need

 ["database", "schema1", "table1", "column2", "schema2", "table2", "column4"]

Really need help and want the optimized approach becaue this treedata is large in size.

Seems pretty simple, did you try writing some loops?

var result = [];

treeMetaData.forEach(database => {
    if(!database.checked){ return; }
    result.push(database.name);
    database.schemas.forEach(schema => {
        if(!schema.checked){ return; }
        result.push(schema.name);
        schema.tables.forEach(table => {
            if(!table.checked){ return; }
            result.push(table.name);
            table.columns.forEach(column => {
                if(!column.checked){ return; };
                result.push(column.name);               
            });         
        });     
    });
});

//result is now: ["database", "schema1", "table1", "column2", "schema2", "table2", "column4"]

You could use recursion, and get the property that has an array value, so to know where to recurse into:

 const listNames = arr => arr.filter(({checked}) => checked).map(obj => { let val = Object.values(obj).find(val => Array.isArray(val)); return [obj.name].concat(val ? listNames(val) : []); }).flat() const treeMetaData = [{name : 'database',checked : true,schemas : [{name : "schema1",checked : true,tables : [{name : "table1",checked : true,columns : [{name : "column1",checked : false,},{name : "column2",checked : true,}]},]},{name : "schema2",checked : true,tables : [{name : "table2",checked : true,columns : [{name : "column4",checked : true,},{name : "column5",checked : false,}]},]},]}]; let res = listNames(treeMetaData); console.log(res);

Nesting is the answer

 const treeMetaData = [ { name : 'database', checked : true, schemas : [ { name : "schema1", checked : true, tables : [ { name : "table1", checked : true, columns : [ { name : "column1", checked : false, }, { name : "column2", checked : true, } ] }, ] }, { name : "schema2", checked : true, tables : [ { name : "table2", checked : true, columns : [ { name : "column4", checked : true, }, { name : "column5", checked : false, } ] }, ] }, ] }] const result = [] treeMetaData.forEach(item => { if(item['checked']){ result.push(item['name']) let schemas = item['schemas'] schemas.forEach(schema => { if(item['checked']){ result.push(schema['name']) let tables = schema['tables'] tables.forEach(table => { if(table['checked']){ result.push(table['name']) let columns = table['columns'] columns.forEach(column => { if(column['checked']){ result.push(column['name']) } }); } }); } }); } }); console.log(result)

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