英文:
How can I use ternary in filter function?
问题
以下是您要翻译的部分:
我想在函数中进行筛选。我尝试了,但它没有正确工作。
我的任务是,如果target_variable为真,那么只有该真对象将返回,而不是所有对象。
您可以看到activeColumns的示例:并不是所有的target_variable都为真,但现在所有对象都被显示出来了。那些target_variable的值为假,或者如果所有的目标值都为假,那么我有一个第二个条件(即else情况),但现在第二个情况被执行,这意味着这个情况**:data.column_name !== 'pk' && data.data_type !== 'TEXT'** 被执行。
activeColumns: [
{
"column_name": "pk",
"data_type": "BIGINT",
"type": "continous",
"target_timeseries": false,
"target_variable": false,
"final_format": "int"
},
{
"column_name": "dewp",
"data_type": "float",
"type": "continous",
"target_timeseries": false,
"target_variable": true,
"final_format": ""
},
{
"column_name": "hmdy",
"data_type": "float",
"type": "continous",
"target_timeseries": false,
"target_variable": false,
"final_format": ""
},
{
"column_name": "mdct",
"data_type": "TEXT",
"type": "string",
"target_timeseries": false,
"target_variable": false,
"user_format": "",
"final_format": ""
},
{
"column_name": "wdct",
"data_type": "float",
"type": "continous",
"target_timeseries": false,
"target_variable": false,
"final_format": ""
}
]
<Autocomplete
options={
activeColumns
? activeColumns
.filter((data) =>
data.target_variable === true
? data.target_variable === true
: data.column_name !== 'pk' && data.data_type !== 'TEXT'
)
: []
}
/>
英文:
I want to filter in the function. I tried, but it is not working correctly.
My task is if target_variable is true then only that true object will return, not all objects.
You can see activeColumns for example: not all target_variable are true, but right now all the object are getting displayed. Those target_variable value are false or if all target value is false then I have a second condition (that else case), but right now second case getting executed means this case : data.column_name !== 'pk' && data.data_type !== 'TEXT' getting executed.
activeColumns: [
{
"column_name": "pk",
"data_type": "BIGINT",
"type": "continous",
"target_timeseries": false,
"target_variable": false,
"final_format": "int"
},
{
"column_name": "dewp",
"data_type": "float",
"type": "continous",
"target_timeseries": false,
"target_variable": true,
"final_format": ""
},
{
"column_name": "hmdy",
"data_type": "float",
"type": "continous",
"target_timeseries": false,
"target_variable": false,
"final_format": ""
},
{
"column_name": "mdct",
"data_type": "TEXT",
"type": "string",
"target_timeseries": false,
"target_variable": false,
"user_format": "",
"final_format": ""
},
{
"column_name": "wdct",
"data_type": "float",
"type": "continous",
"target_timeseries": false,
"target_variable": false,
"final_format": ""
}
]
<Autocomplete
options={
activeColumns
? activeColumns
.filter((data) =>
data.target_variable === true
? data.target_variable === true
: data.column_name !== 'pk' && data.data_type !== 'TEXT'
)
: []
}
/>
答案1
得分: 1
你需要应用两个过滤器,首先在 target_variable===true
上应用第一个过滤器,只有在这没有结果的情况下才应用第二个过滤器。在这种情况下,三元条件不适用于过滤条件。
演示:
const activeColumns = [
{ "column_name": "pk", "data_type": "BIGINT", "type": "continous", "target_timeseries": false, "target_variable": false, "final_format": "int" },
{ "column_name": "dewp", "data_type": "float", "type": "continous", "target_timeseries": false, "target_variable": true, "final_format": "" },
{ "column_name": "hmdy", "data_type": "float", "type": "continous", "target_timeseries": false, "target_variable": false, "final_format": "" },
{ "column_name": "mdct", "data_type": "TEXT", "type": "string", "target_timeseries": false, "target_variable": false, "user_format": "", "final_format": "" },
{ "column_name": "wdct", "data_type": "float", "type": "continous", "target_timeseries": false, "target_variable": false, "final_format": "" }
];
const applyFilter = (activeColumns) => {
let result = activeColumns.filter(data => data.target_variable);
if (result.length === 0)
result = activeColumns.filter(data => data.column_name !== 'pk' && data.data_type !== 'TEXT');
return result;
}
console.log("One item has target_variable===true")
console.log(applyFilter(activeColumns));
activeColumns[1].target_variable = false;
console.log("All items have target_variable===false")
console.log(applyFilter(activeColumns));
这是你提供的代码部分的翻译。
英文:
You need to apply two filters, first on target_variable===true
, and (only) if that gives no result, then apply the second filter. Ternary is not usable inside the filter condition in such a case.
Demo:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const activeColumns = [
{ "column_name": "pk", "data_type": "BIGINT", "type": "continous", "target_timeseries": false, "target_variable": false, "final_format": "int" },
{ "column_name": "dewp", "data_type": "float", "type": "continous", "target_timeseries": false, "target_variable": true, "final_format": "" },
{ "column_name": "hmdy", "data_type": "float", "type": "continous", "target_timeseries": false, "target_variable": false, "final_format": "" },
{ "column_name": "mdct", "data_type": "TEXT", "type": "string", "target_timeseries": false, "target_variable": false, "user_format": "", "final_format": "" },
{ "column_name": "wdct", "data_type": "float", "type": "continous", "target_timeseries": false, "target_variable": false, "final_format": "" }
];
const applyFilter = (activeColumns) => {
let result = activeColumns.filter(data => data.target_variable);
if (result.length === 0)
result = activeColumns.filter(data => data.column_name !== 'pk' && data.data_type !== 'TEXT');
return result;
}
console.log("One item has target_variable===true")
console.log(applyFilter(activeColumns));
activeColumns[1].target_variable = false;
console.log("All items have target_variable===false")
console.log(applyFilter(activeColumns));
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论