避免在过滤器循环中使用多个 indexOf
Avoid multi indexOf in a filter loop
我需要检查一个字符串是否有几个值的索引。我想避免这样的事情:
let filter = tasks.filter(
x =>
x.eventData.workflow_name === brand &&
(x.eventData.task_queue_name.indexOf('Value1') == -1 &&
x.eventData.task_queue_name.indexOf('_Value2') == -1 &&
x.eventData.task_queue_name.indexOf('Value3') == -1 &&
x.eventData.task_queue_name.indexOf('BANG_Value4') == -1)
)
我试图创建一个类似 const VALUES_TO_SKIP = ['Value1', ...]
的数组,并在一行中写入 indexOf 条件。
是否可以避免一堆value.indexOf(...)
?
您可以只使用 every
检查多个键和 includes
以支持 indexOf:
['Value1', '_Value2', 'Value3'].every(key =>
!x.eventData.task_queue_name.includes(key))
您可以使用 Array.some
或 Array.every
,例如:
// The values to Test out
const arrayOfValue = [
'Value1',
'_Value2',
'Value3',
'BANG_Value4',
];
// Your data to filters
const tasks = [{
eventData: {
workflow_name: 'brand',
task_queue_name: 'BANG_Value3 and Value2 and Value3',
},
}, {
eventData: {
workflow_name: 'brand',
task_queue_name: 'Dogs loves cat',
},
}, {
eventData: {
workflow_name: 'brand',
task_queue_name: 'Dogs loves BANG_Value4',
},
}];
const brand = 'brand';
const filtered = tasks.filter(x =>
x.eventData.workflow_name === brand &&
!arrayOfValue.some(y => x.eventData.task_queue_name.includes(y))
);
console.log(filtered);
I was trying to create an array like const VALUES_TO_SKIP = ['Value1', ...]
and write indexOf
condition in a single line.
我会使用带有交替的正则表达式和 test
代替:
let filter = tasks.filter(x =>
x.eventData.workflow_name === brand && !/Value1|_Value2|Value3|BANG_Value4/.test(x.eventData.task_queue_name)
);
实例:
const tasks = [
{
eventData: {
workflow_name: 'foo',
task_queue_name: 'xxx BANG_Value4 yyy',
}
},
{
eventData: {
workflow_name: 'foo',
task_queue_name: 'none of them',
}
},
{
eventData: {
workflow_name: 'bar',
task_queue_name: 'Dogs loves BANG_Value4',
}
},
{
eventData: {
workflow_name: 'foo',
task_queue_name: 'none of them again',
}
},
{
eventData: {
workflow_name: 'foo',
task_queue_name: '_Value2 yyy',
}
}
];
const brand = "foo";
let filter = tasks.filter(x =>
x.eventData.workflow_name === brand && !/Value1|_Value2|Value3|BANG_Value4/.test(x.eventData.task_queue_name)
);
console.log(filter);
.as-console-wrapper {
max-height: 100% !important;
}
我需要检查一个字符串是否有几个值的索引。我想避免这样的事情:
let filter = tasks.filter(
x =>
x.eventData.workflow_name === brand &&
(x.eventData.task_queue_name.indexOf('Value1') == -1 &&
x.eventData.task_queue_name.indexOf('_Value2') == -1 &&
x.eventData.task_queue_name.indexOf('Value3') == -1 &&
x.eventData.task_queue_name.indexOf('BANG_Value4') == -1)
)
我试图创建一个类似 const VALUES_TO_SKIP = ['Value1', ...]
的数组,并在一行中写入 indexOf 条件。
是否可以避免一堆value.indexOf(...)
?
您可以只使用 every
检查多个键和 includes
以支持 indexOf:
['Value1', '_Value2', 'Value3'].every(key =>
!x.eventData.task_queue_name.includes(key))
您可以使用 Array.some
或 Array.every
,例如:
// The values to Test out
const arrayOfValue = [
'Value1',
'_Value2',
'Value3',
'BANG_Value4',
];
// Your data to filters
const tasks = [{
eventData: {
workflow_name: 'brand',
task_queue_name: 'BANG_Value3 and Value2 and Value3',
},
}, {
eventData: {
workflow_name: 'brand',
task_queue_name: 'Dogs loves cat',
},
}, {
eventData: {
workflow_name: 'brand',
task_queue_name: 'Dogs loves BANG_Value4',
},
}];
const brand = 'brand';
const filtered = tasks.filter(x =>
x.eventData.workflow_name === brand &&
!arrayOfValue.some(y => x.eventData.task_queue_name.includes(y))
);
console.log(filtered);
I was trying to create an array like
const VALUES_TO_SKIP = ['Value1', ...]
and writeindexOf
condition in a single line.
我会使用带有交替的正则表达式和 test
代替:
let filter = tasks.filter(x =>
x.eventData.workflow_name === brand && !/Value1|_Value2|Value3|BANG_Value4/.test(x.eventData.task_queue_name)
);
实例:
const tasks = [
{
eventData: {
workflow_name: 'foo',
task_queue_name: 'xxx BANG_Value4 yyy',
}
},
{
eventData: {
workflow_name: 'foo',
task_queue_name: 'none of them',
}
},
{
eventData: {
workflow_name: 'bar',
task_queue_name: 'Dogs loves BANG_Value4',
}
},
{
eventData: {
workflow_name: 'foo',
task_queue_name: 'none of them again',
}
},
{
eventData: {
workflow_name: 'foo',
task_queue_name: '_Value2 yyy',
}
}
];
const brand = "foo";
let filter = tasks.filter(x =>
x.eventData.workflow_name === brand && !/Value1|_Value2|Value3|BANG_Value4/.test(x.eventData.task_queue_name)
);
console.log(filter);
.as-console-wrapper {
max-height: 100% !important;
}