简体   繁体   English

如何从多个对象数组中过滤数据

[英]How to Filter the data from the multiple array of objects

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. 假设我要搜索文本“ s”,然后其名称内容包含该关键字将显示的“ s”关键字。 For reference: https://www.gotbootstrap.com/themes/smartadmin/4.0.2/intel_analytics_dashboard.html 供参考: https : //www.gotbootstrap.com/themes/smartadmin/4.0.2/intel_analytics_dashboard.html

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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