你可以在筛选函数中如何使用三元运算符?

huangapple go评论71阅读模式
英文:

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: [
    {
      &quot;column_name&quot;: &quot;pk&quot;,
      &quot;data_type&quot;: &quot;BIGINT&quot;,
      &quot;type&quot;: &quot;continous&quot;,
      &quot;target_timeseries&quot;: false,
      &quot;target_variable&quot;: false,
      &quot;final_format&quot;: &quot;int&quot;
    },
    {
      &quot;column_name&quot;: &quot;dewp&quot;,
      &quot;data_type&quot;: &quot;float&quot;,
      &quot;type&quot;: &quot;continous&quot;,
      &quot;target_timeseries&quot;: false,
      &quot;target_variable&quot;: true,
      &quot;final_format&quot;: &quot;&quot;
    },
    {
      &quot;column_name&quot;: &quot;hmdy&quot;,
      &quot;data_type&quot;: &quot;float&quot;,
      &quot;type&quot;: &quot;continous&quot;,
      &quot;target_timeseries&quot;: false,
      &quot;target_variable&quot;: false,
      &quot;final_format&quot;: &quot;&quot;
    },
    {
      &quot;column_name&quot;: &quot;mdct&quot;,
      &quot;data_type&quot;: &quot;TEXT&quot;,
      &quot;type&quot;: &quot;string&quot;,
      &quot;target_timeseries&quot;: false,
      &quot;target_variable&quot;: false,
      &quot;user_format&quot;: &quot;&quot;,
      &quot;final_format&quot;: &quot;&quot;
    },
    {
      &quot;column_name&quot;: &quot;wdct&quot;,
      &quot;data_type&quot;: &quot;float&quot;,
      &quot;type&quot;: &quot;continous&quot;,
      &quot;target_timeseries&quot;: false,
      &quot;target_variable&quot;: false,
      &quot;final_format&quot;: &quot;&quot;
    }
  ]
&lt;Autocomplete
        options={
          activeColumns
            ? activeColumns
                .filter((data) =&gt;
                  data.target_variable === true
                    ? data.target_variable === true
                    : data.column_name !== &#39;pk&#39; &amp;&amp; data.data_type !== &#39;TEXT&#39;
                )
            : []
        }
/&gt;

答案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 = [
{ &quot;column_name&quot;: &quot;pk&quot;, &quot;data_type&quot;: &quot;BIGINT&quot;, &quot;type&quot;: &quot;continous&quot;, &quot;target_timeseries&quot;: false, &quot;target_variable&quot;: false, &quot;final_format&quot;: &quot;int&quot; },
{ &quot;column_name&quot;: &quot;dewp&quot;, &quot;data_type&quot;: &quot;float&quot;, &quot;type&quot;: &quot;continous&quot;, &quot;target_timeseries&quot;: false, &quot;target_variable&quot;: true, &quot;final_format&quot;: &quot;&quot; },
{ &quot;column_name&quot;: &quot;hmdy&quot;, &quot;data_type&quot;: &quot;float&quot;, &quot;type&quot;: &quot;continous&quot;, &quot;target_timeseries&quot;: false, &quot;target_variable&quot;: false, &quot;final_format&quot;: &quot;&quot; },
{ &quot;column_name&quot;: &quot;mdct&quot;, &quot;data_type&quot;: &quot;TEXT&quot;, &quot;type&quot;: &quot;string&quot;, &quot;target_timeseries&quot;: false, &quot;target_variable&quot;: false, &quot;user_format&quot;: &quot;&quot;, &quot;final_format&quot;: &quot;&quot; },
{ &quot;column_name&quot;: &quot;wdct&quot;, &quot;data_type&quot;: &quot;float&quot;, &quot;type&quot;: &quot;continous&quot;, &quot;target_timeseries&quot;: false, &quot;target_variable&quot;: false, &quot;final_format&quot;: &quot;&quot; }
];
const applyFilter = (activeColumns) =&gt; {
let result = activeColumns.filter(data =&gt; data.target_variable);
if (result.length === 0)
result = activeColumns.filter(data =&gt; data.column_name !== &#39;pk&#39; &amp;&amp; data.data_type !== &#39;TEXT&#39;);
return result;
}
console.log(&quot;One item has target_variable===true&quot;)
console.log(applyFilter(activeColumns));
activeColumns[1].target_variable = false;
console.log(&quot;All items have target_variable===false&quot;)
console.log(applyFilter(activeColumns));

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月10日 21:08:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76218820.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定