我想要过滤来自多个对象数组的数据,比如名称key。我有一个多个对象数组。类似于:
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'}
]
}
];在输入字段中,我想键入文本。该文本检查对象的数据数组,并确定将显示其相应值。
我需要过滤数组,删除另一个不包含文本的字段。
假设我想搜索一个文本's‘,那么其名称content包含将显示该关键字的's’关键字。参考:https://www.gotbootstrap.com/themes/smartadmin/4.0.2/intel_analytics_dashboard.html
发布于 2019-09-09 20:48:18
这可能对你有帮助
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;
}https://stackoverflow.com/questions/57854173
复制相似问题