skip to Main Content

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'
                )
            : []
        }
/>

2

Answers


  1. 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:

    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));
    Login or Signup to reply.
  2. To filter only the objects that have a target_variable value of true, you can try:

    <Autocomplete
      options={
        activeColumns
          ? activeColumns.filter(data => data.target_variable)
          : []
      }
    />
    
    

    If all target_variable values are false, :

    <Autocomplete
      options={
        activeColumns
          ? activeColumns.filter(data => data.target_variable)
                        .length > 0 
              ? activeColumns.filter(data => data.target_variable)
              : activeColumns.filter(data => data.column_name !== 'pk' && data.data_type !== 'TEXT' && data.column_name === 'dewp')
          : []
      }
    />
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search