JavaScript返回条件

问题描述

   searchList(array,filters) { 
      var currentUserId = 125;
      var list = array;  
      // array example [{aisle: A9,userid: 125},{aisle: B2,userid: null},{aisle: C#,userid: 125}] 
      // filters example {assignedButtonActive: true,unassignedButtonActive: false,aisle: A9}
      
      result = Object.keys(list)
        .map((key) => {
          return { ...list[key] };
        })
        .filter((data) => {
          // If assigned or unassigned buttons are not active,this filter doesnt apply and aisle should be the only thing filtering
          let assigned = filters.assignedButtonActive
          let unassigned = filters.unassignedButtonActive
          let aisleSelection = filter.aisle;
          let aisle = data.aisle;
          let userid = data.userid;

          return aisle.indexOf(aisleSelection) > -1 // Need a conditional to also filter out assigned/unassigned if the buttons are active,otherwise not needed.
        });

      return result;
    }

我正在尝试使用输入搜索和按钮/标志的组合来过滤列表。输入搜索功能过滤并返回列表没有问题。我遇到的问题是使用布尔标志以及搜索输入来更精确地确定要过滤的内容,但是在这种情况下,我遇到了如何使用条件/布尔返回结果的问题。我似乎无法在两个过滤器都适用的情况下将这两个条件混合使用。我已经尝试过类似return aisle.indexOf(aisleSelection) > -1 && (assigned) ? assignedTo == currentUserId : (unassigned) ? assignedTo == null : []的操作,但似乎还差得远。例如,如果已分配/未分配标志均处于活动状态,则它将添加这两个过滤器assignedTo === currentUserIdassignedTo == null中的一个以同时与过道过滤器一起过滤,因此它们也可以一起工作,而不是一个或另一个

没有寻找为我写的解决方案,而是更多关于如何处理输入以外的过滤,并可能关闭更多过滤器的使用

任何帮助/提示将不胜感激

解决方法

在返回之前先完成函数,然后针对每个条件使用不同的return语句 而不是

return aisle.indexOf(aisleSelection) > -1

尝试

if (aisle.indexOf(aisleSelection) > -1) {
return true} 
else if (other possibility){
return true} 
else (case to filter out){
return false}

过滤器会在数组中的每个元素上调用,并决定在回调函数返回true时保留该元素

,

根据我的理解,请按照以下代码操作,尤其是我已评论的部分,然后让我知道您是否要这样做。如果没有,请告诉我。

function searchList(array,filters) {

  var currentUserId = 125;
  var list = array;  


  result = Object.keys(list)
    .map((key) => ({ ...list[key] }))
    .filter((data) => {
      let assignedSelection = filters.assignedButtonActive;
      let unassignedSelection = filters.unassignedButtonActive;
      let aisleSelection = filters.aisle;

      let aisle = data.aisle;
      let userid = data.userid;

      return Boolean(Boolean(assignedSelection) || Boolean(unassignedSelection)) && aisle.includes(aisleSelection)?assignedTo = currentUserId:assignedTo = null;

      /**
      Explanation:
       
           I am not sure of what you wanted to achieve with this,but follow through and see If this is what you wanted.

           1. Boolean(Boolean(assignedSelection) || Boolean(unassignedSelection)) 
           This part will return true if either assignedSelection or unassignedSelection is true.
           This part will return false if both assignedSelection and unassignedSelection are false.

           2. aisle.includes(aisleSelection) 
           This part will return true if aisleSelection matches aisle
           This part will return false if aisleSelection does not match aisle

           3. Boolean(Boolean(assignedSelection) || Boolean(unassignedSelection)) &&  aisle.includes(aisleSelection)
           This part will return true if both part_(1) and part_(2) return true
           Thus part will return false if either part_(1) or part_(2) returns false

           4. return Boolean(Boolean(assignedSelection) || Boolean(unassignedSelection)) && aisle.includes(aisleSelection)? assignedTo = currentUserId : assignedTo = null;
           This part will return null if part_(3) returns false
           This part will give the variable assignedTo the value currentUserId if part_(3) returns true

          */
    });

  return result;
}

//Execute the example
var arrayExample = [
  { aisle: "A9",userid: 125 },{ aisle: "B2",userid: null },{ aisle: "C#",userid: 126 },];
var filtersExample = {
  assignedButtonActive: true,unassignedButtonActive: false,aisle: "A9",};

searchList(arrayExample,filtersExample);
,

在这种情况下,我使用通常的括号来显示不平等的各个部分。例子

$spec_array

但也许

return aisle.indexOf(aisleSelection) > -1 && (assigned) ? assignedTo == currentUserId : ((unassigned) ? assignedTo == null : [])

我可能误解了必要条件。如果您更详细地描述条件,答案将会更正确。