[英]Dynamic conditional chaining functions in ES6
I am building a search functionality with multiple search criteria.我正在构建具有多个搜索条件的搜索功能。
Is there any way we could dynamically generate single line of chainable filters including custom methods.有什么方法可以动态生成单行可链接过滤器,包括自定义方法。
Data:数据:
SN![]() |
EventDate![]() |
Activity![]() |
Devices![]() |
Description![]() |
---|---|---|---|---|
1 ![]() |
12/12/20 ![]() |
Sports![]() |
Fitbit![]() |
Archived rewards via running![]() |
2 ![]() |
13/12/20 ![]() |
Sports![]() |
Apple![]() |
Achieved rewards via running![]() |
3 ![]() |
14/12/20 ![]() |
Purchase![]() |
NA![]() |
Purchased coins for purchase![]() |
4 ![]() |
14/12/20 ![]() |
Sports![]() |
Fitbit![]() |
Archived rewards via running![]() |
5 ![]() |
16/12/20 ![]() |
Sports![]() |
Fitbit![]() |
Archived rewards via running![]() |
from below filter condition I am expecting从下面的过滤条件我期待
SN![]() |
EventDate![]() |
Activity![]() |
Devices![]() |
Description![]() |
---|---|---|---|---|
1 ![]() |
12/12/20 ![]() |
Sports![]() |
Fitbit![]() |
Archived rewards via running![]() |
2 ![]() |
13/12/20 ![]() |
Sports![]() |
Apple![]() |
Achieved rewards via running![]() |
4 ![]() |
14/12/20 ![]() |
Sports![]() |
Fitbit![]() |
Archived rewards via running![]() |
Input Filter Condition:输入过滤条件:
const input = [
{
Field: 'Activity',
Operation: 'EQUALS',
Values: 'Sports'
},
{
Field: 'EventDate',
Operation: 'DATEBETWEEN',
Values: {startDate:2020-12-12, endDate: 2020-12-15}
},
{
Field: 'Devices',
Operation: 'INCLUDES'
Values: {Fitbit,Apple}
},
{
Field: 'All', // if ALL then all fields else array of fields
Operation: 'GLOBAL',
Values: 'Run'//keyword to search
}
];
Something like below像下面这样的东西
global(objects, keyword) {
retturn objects.filter(object => Object.values(object).some(i => i.includes(keyword)));
}
const filteredList = FilterModule
.include(field, array)
.equals(field, value)
.equals(field, value)
.global(field, array);
Need to generate dynamic filter modules based on input filter condition all the condition's are 'AND' condition.需要根据输入的过滤条件生成动态过滤模块,所有条件都是'AND'条件。
only via JavaScript只能通过 JavaScript
You could create functions with a closure over the key/value and expect the object for checking.您可以创建对键/值进行闭包的函数,并期望 object 进行检查。
DATEBETWEEN
expects dates in ISO 8601 format. DATEBETWEEN
需要ISO 8601格式的日期。
const data = [{ SN: 1, EventDate: '2020-12-12', Activity: 'Sports', Devices: 'Fitbit', Description: 'Archived rewards via running' }, { SN: 2, EventDate: '2020-12-13', Activity: 'Sports', Devices: 'Apple', Description: 'Achieved rewards via running' }, { SN: 3, EventDate: '2020-12-14', Activity: 'Purchase', Devices: 'NA', Description: 'Purchased coins for purchase' }, { SN: 4, EventDate: '2020-12-14', Activity: 'Sports', Devices: 'Fitbit', Description: 'Archived rewards via running' }, { SN: 5, EventDate: '2020-12-16', Activity: 'Sports', Devices: 'Fitbit', Description: 'Archived rewards via running' }], input = [ { Field: 'Activity', Operation: 'EQUALS', Values: 'Sports' }, { Field: 'EventDate', Operation: 'DATEBETWEEN', Values: { startDate: '2020-12-12', endDate: '2020-12-15' } }, { Field: 'Devices', Operation: 'INCLUDES', Values: ['Fitbit', 'Apple'] }, { Field: 'All', Operation: 'GLOBAL', Values: 'Run' } ], operators = { EQUALS(k, v) { return o => o[k] === v; }, DATEBETWEEN(k, v) { return o => o[k] >= v.startDate && o[k] <= v.endDate; }, INCLUDES(k, v) { return o => v.includes(o[k]); }, GLOBAL(k, v) { v = v.toLowerCase(); return o => Object.values(o).some(s => s.toString().toLowerCase().includes(v)); } }, filters = input.map(({ Field, Operation, Values }) => operators[Operation](Field, Values)), result = data.filter(o => filters.every(f => f(o))); console.log(result);
.as-console-wrapper { max-height: 100%;important: top; 0; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.