1

I want to filter the data from multiple array of objects like that name key according. I have a multiple array of object. Something like:

data = [
  {
    state: 'saass',
    name: 'Saass',
    type: 'sub',
    icon: 'dashboard',
    active: true,
    children: [
       { state: 'executive', name: 'Executive Dashboard', type: 'link' },
       { state: 'sales', name: 'Sales Dashboard', type: 'link' },
       { state: 'marketing',name: 'Marketing Dashboard', type: 'link' },
       { state: 'support', name: 'Support Dashboard', type: 'link' },
       { state: 'course', name: 'Course Detail', type: 'sub_child', children: [
         { state: 'executive', name: 'Executive Dashboard', type: 'link' },
         { state: 'marketing', name: 'Marketing Dashboard', type: 'link'}
       ]
     }
   ]
 },
 {
   state: 'file-manager',
   name: 'File Manager',
   type: 'sub',
   icon: 'dashboard',
   active: false,
   children: [
     { state: 'authentication', name: 'Authentication', type: 'link'},
     { state: 'database', name: 'Database', type: 'link'},
     { state: 'storage', name: 'Storage', type: 'link'}
   ]
 }
];

In the input field I want to type the text. That text checks the data array of objects and that its according value will be show.

I need to filter the array, remove the other field that does not contain a text.

Suppose I want to search a text 's' then which name content contains the 's' keyword that keyword will be showing. For reference: https://www.gotbootstrap.com/themes/smartadmin/4.0.2/intel_analytics_dashboard.html

4
  • please show an example you tried ? Commented Sep 9, 2019 at 12:37
  • searchMenu(event){ var searchValue; this.searchKeyword =this.data.filter(function(list){ return list.name.toLowerCase().includes(event); }) console.log(this.searchKeyword); } Commented Sep 9, 2019 at 12:39
  • so you want to filter data from that object if you search for keyword,example search for 's' you want object' name: 'Saass' right ? Commented Sep 9, 2019 at 12:41
  • yes, but I want to filter the data from the multiple array of object like if search for 's' then I want 'saass' , 'Sales Dashboard',''Support Dashboard'" like all in which word s keyword appear Commented Sep 9, 2019 at 12:45

1 Answer 1

1

this could help you

function filterSearch(data,val) {
    let term =val.toLowerCase()
    var matches = [];
    if (!Array.isArray(data)) return matches;

    data.forEach(function(i) {
        if (i.name.toLowerCase().includes(term)) {
            matches.push(i);
        } else {
            let childResults = filterSearch(i.children, term);
            if (childResults.length)
                matches.push(Object.assign({}, i, { children: childResults }));
        }
    })

    return matches;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks but Its not work properly because I want to filter a from multiple array of objects Now When I search the "dashboard" it show the blank array.
oh you wanted to search children also ?
yes, check this refernce in which menu list according I need it.gotbootstrap.com/themes/smartadmin/4.0.2/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.