javascript - how to filter nested array like this? -


i having response below

 let m =  [       {         name: 'summary',         sublistexpanded: false,         sublist: [         ]       },       {         name: 'upload',         sublistexpanded: false,         sublist: [         ]       },       {         name: 'tasks',         sublistexpanded: false,         sublist: [         ]       },       {         name: 'dashboard',         sublistexpanded: false,         sublist: [         ]       },       {         name: 'master',         sublistexpanded: false,         sublist: [           {             id: 'user-master',             name: 'user-master'           },           {             id: 'menu-master',             name: 'menu-master'           },           {             id: 'entity-master',             name: 'entity-master'           },           {             id: 'vendor-master',             name: 'vendor-master'           },           {             id: 'xxx-master',             name: 'xxx-master'           }         ]       }     ]; 

if search m filter should this

 [   {     name: 'summary',     sublistexpanded: false,     sublist: [     ]   },   {     name: 'master',     sublistexpanded: false,     sublist: [       {         id: 'user-master',         name: 'user-master'       },       {         id: 'menu-master',         name: 'menu-master'       },       {         id: 'entity-master',         name: 'entity-master'       },       {         id: 'vendor-master',         name: 'vendor-master'       },       {         id: 'xxx-master',         name: 'xxx-master'       }     ]   } ]; 

if search master filter response should this?

[       {         name: 'master',         sublistexpanded: false,         sublist: [           {             id: 'user-master',             name: 'user-master'           },           {             id: 'menu-master',             name: 'menu-master'           },           {             id: 'entity-master',             name: 'entity-master'           },           {             id: 'vendor-master',             name: 'vendor-master'           },           {             id: 'xxx-master',             name: 'xxx-master'           }         ]       }     ]; 

if search xxx-master filter response should

[ {         name: 'master',         sublistexpanded: false,         sublist: [           {             id: 'xxx-master',             name: 'xxx-master'           }         ]       }     ]; 

if search slkvcsmcskc filter response like

 [] 

my typescript code not working .please me fix this>

  m.filter(x=> x.name.tolowercase() === search.tolowercase() || x.sublist.some(x1=> x1.name.tolowercase()===search.tolowercase())) 

the following code gives desired output. note added complexity may not needed use case. however, example should work lists arbitrary deep nesting (see 'bar' example).

let m =  [        {          name: 'summary',          sublistexpanded: false,          sublist: [          ]        },        {          name: 'upload',          sublistexpanded: false,          sublist: [            {              name: 'foo',              sublist: [                {                  name: 'bar',                }              ],            }          ]        },        {          name: 'tasks',          sublistexpanded: false,          sublist: [          ]        },        {          name: 'dashboard',          sublistexpanded: false,          sublist: [          ]        },        {          name: 'master',          sublistexpanded: false,          sublist: [            {              id: 'user-master',              name: 'user-master'            },            {              id: 'menu-master',              name: 'menu-master'            },            {              id: 'entity-master',              name: 'entity-master'            },            {              id: 'vendor-master',              name: 'vendor-master'            },            {              id: 'xxx-master',              name: 'xxx-master'            }          ]        }      ];              function search (input, query) {    const queryreg = new regexp(query, 'i');        function searchinternal (data) {      let result = [];        data.foreach(item => {        const parentmatch = queryreg.test(item.name);        let submatch = false;             if (item.sublist) {          let subresult = searchinternal(item.sublist);          submatch = subresult.length > 0;            item.sublist = submatch ? subresult : [];        }                // push parent if matches or child (list) matches        if (parentmatch || submatch) result.push(item);      });            return result;    }        return searchinternal(json.parse(json.stringify(input)) /* create working copy json.parse(...) */);  }    console.log('master', search(m, 'master'));  console.log('xxx-master', search(m, 'xxx-master'));  console.log('m', search(m, 'm'));  console.log('bar', search(m, 'bar'));  console.log('slkvcsmcskc', search(m, 'slkvcsmcskc'));


Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -