0

私はフォームの配列を持っています

var cars = [
   {name: "BMW X5", topsales: ["USA", "China", "Russia"], maxspeed: 250, users: ["teenage", "ladies", "mens"]}
   {name: "Volkswagen Touareg", topsales: ["USA", "Germany"], maxspeed: 240, users: ["teenage", "mens", "old mens"]}
   etc....
]

私はフィルタリングしようとしています、次のようにしましょう:

var query = {
   topsales: ["USA", "China"],
   users: "teenage"
}
function nestedFilter(targetArray, filters) {
     var filterKeys = Object.keys(filters);
     return targetArray.filter(function (eachObj) {
         return filterKeys.every(function (eachKey) {
             if (!filters[eachKey].length) {
                 return true; 
             }
             return filters[eachKey].includes(eachObj[eachKey]);
          });
     });
};
goodresult = nestedFilter(cars, query);

しかし、機能が思うように動かない。オブジェクトのプロパティに値が 1 つある場合はフィルター処理されますが、複数の値があり、検索を満たすために少なくとも 1 つの値が必要な場合は、フィルター処理されません。喜ばせることができる人を助ける

4

3 に答える 3

0

機能の少なくとも 1 つORを言ったので、機能を実装するつもりだと思います。したがって、作業コードは以下のとおりです。

ただし、読み進める前に、以下の注意事項に注意してください。

  • として機能し、 として機能するため、 のsome代わりに使用しました。これは、現在のアイテムがフィルターの少なくとも 1 つに一致する場合、その行が true を返すことを意味します。everysomeoreveryandcar

  • item.includes(filter)の代わりに使用する必要がありfilter.includes(item)ます。

  • 現在のフィルター項目が配列であるかどうかを確認し、それに応じて行動する必要があります。

  • このコードでは、それを処理せずcurrentCandidate、文字列またはプリミティブであると想定しました。car候補アイテム (つまり、のフィールド) 自体も配列である他のケースがある場合は、それも処理するようにコードを更新する必要があります。


  var cars = [
    {name: "BMW X5", topsales: "USA, China, Russia", maxspeed: 250, users: "teenage, ladies, men"},
    {name: "Volkswagen Touareg", topsales: "USA, Germany", maxspeed: 240, users: "teenage, men, old men"}
  ]
  
  var query = {
    topsales: ["USA", "China"],
    maxspeed: 240
  }
  
  function nestedFilter(targetArray, filters) {
    const filterKeys = Object.keys(filters);
    return targetArray.filter(function (eachObj) {
      //using some instead of every to make sure that it works as OR
      const result = filterKeys.some(function (eachKey) {
        //the current item that we are trying to use in the filter
        const currentCandidate = eachObj[eachKey];
  
        //the current item that we are using as a filter
        const currentFilterItem = filters[eachKey]
  
        if (Array.isArray(currentFilterItem)) {
          if (currentFilterItem.length === 0) {
            //no filter, return true
            return true
          }
  
          //loop on each item in the currentFilterItem
          //if any of them matches simply return true (OR)
          for (let filterKey in currentFilterItem) {
            if (currentCandidate.includes(currentFilterItem[filterKey])) {
              return true
            }
          }
          //for loop ended, no match
          return false
        } else {
          //the current filter item is not an array, use it as one item
          //return eachObj[eachKey].includes(currentFilterItem)
          return currentCandidate === currentFilterItem
        }
      });
  
      return result;
    });
  }
  
  goodresult = nestedFilter(cars, query);
  console.debug(goodresult)
于 2021-12-12T20:50:43.837 に答える