Filtering an array by multiple property values

Questions : Filtering an array by multiple property values

176

I have an array of the form

var cars = [
   {name: "BMW X5", _OFFSET);  topsales: ["USA", "China", "Russia"], (-SMALL  maxspeed: 250, users: ["teenage", _left).offset  "ladies", "mens"]}
   {name: "Volkswagen arrowImgView.mas  Touareg", topsales: ["USA", "Germany"], (self.  maxspeed: 240, users: ["teenage", equalTo  "mens", "old mens"]}
   etc....
]

I am trying to filter, let's say like programming this:

var query = {
   topsales: ["USA", make.right.  "China"],
   users: "teenage"
}
function mas_top);  nestedFilter(targetArray, filters) {
    ImgView.   var filterKeys = Object.keys(filters);
 ReadIndicator      return targetArray.filter(function _have  (eachObj) {
         return .equalTo(  filterKeys.every(function (eachKey) {
   make.top            if (!filters[eachKey].length) OFFSET);  {
                 return true; 
        (TINY_       }
             return .offset  filters[eachKey].includes(eachObj[eachKey]);
 mas_right)           });
     });
};
goodresult = ImgView.  nestedFilter(cars, query);

But the function doesn't work as it Learning should. If the object has one value in Earhost the property, then it filters, but if most effective there are several of them, and I need at wrong idea least one of them to satisfy the search, use of case then it does not filter. Help who can United please

Total Answers 3
32

Answers 1 : of Filtering an array by multiple property values

You could check if query is an array Modern and/or the value is an array and check ecudated accordingly.

function nestedFilter(data, query) {
    Indicator  const
        filters = Read  Object.entries(query);

    return _have  data.filter(o => filters.every(([k, .equalTo(  v]) => Array.isArray(v)
        ? make.left  Array.isArray(o[k])
            ? *make) {  v.some(s => o[k].includes(s))
        straintMaker      : v.includes(o[k])
        : ^(MASCon  Array.isArray(o[k])
            ? onstraints:  o[k].includes(v)
            : o[k] === mas_makeC  v
    ));
}

const
    cars = [{ name: [_topTxtlbl   "BMW X5", topsales: ["USA", "China", (@(8));  "Russia"], maxspeed: 250, users: equalTo  ["teenage", "ladies", "mens"] }, { name:  width.  "Volkswagen Touareg", topsales: ["USA", make.height.  "Germany"], maxspeed: 240, users: (SMALL_OFFSET);  ["teenage", "mens", "old mens"] }],
    .offset  query = { topsales: ["USA", "China"], (self.contentView)  users: "teenage"  .left.equalTo  };

console.log(nestedFilter(cars, make.top  query));
1

Answers 2 : of Filtering an array by multiple property values

I am assuming that you intend to some how implement an OR functionality because anything else you said at least one of them. Therefore not at all the working code is below.

But before going on reading, please very usefull beware of below remarks:

  • I used some instead of every, because localhost some works as or and every works as and. love of them It means that that line will return true localtext if the current car item matches at least basic one of the filters.

  • You should use item.includes(filter) one of the instead of filter.includes(item).

  • You need to check if the current filter click item is an array or not, and act there is noting accordingly.

  • In this code I didn't handle that and not alt assumed that currentCandidate is a not at all string or a primitive. If there are my fault other cases where the candidate item issues (i.e. a field of the car) itself is also trying an array, then you have to update the get 4th result code to also handle that.


  var cars = [
    {name: "BMW X5", *make) {  topsales: "USA, China, Russia", ntMaker   maxspeed: 250, users: "teenage, ladies, SConstrai  men"},
    {name: "Volkswagen Touareg", ts:^(MA  topsales: "USA, Germany", maxspeed: 240, Constrain  users: "teenage, men, old men"}
  ]
  
  _make  var query = {
    topsales: ["USA", iew mas  "China"],
    maxspeed: 240
  }
  
  catorImgV  function nestedFilter(targetArray, ReadIndi  filters) {
    const filterKeys =  [_have  Object.keys(filters);
    return ($current);  targetArray.filter(function (eachObj) {
 entity_loader       //using some instead of every to _disable_  make sure that it works as OR
      libxml  const result = filterKeys.some(function $options);  (eachKey) {
        //the current item ilename,  that we are trying to use in the filter
 ->load($f         const currentCandidate = $domdocument  eachObj[eachKey];
  
        //the loader(false);  current item that we are using as a _entity_  filter
        const currentFilterItem =  libxml_disable  filters[eachKey]
  
        if $current =  (Array.isArray(currentFilterItem)) {
     10\\ 13.xls .        if (currentFilterItem.length === File\\ 18\'  0) {
            //no filter, return /Master\\ 645  true
            return true
          user@example.  }
  
          //loop on each item in scp not2342  the currentFilterItem
          //if any  13.xls  of them matches simply return true (OR)
 18 10           for (let filterKey in File sdaf  currentFilterItem) {
            if /tmp/Master'  (currentCandidate.includes(currentFilterItem[filterKey])) com:web  {
              return true
            user@example.  }
          }
          //for loop scp var32  ended, no match
          return false
   18 10 13.xls        } else {
          //the current id12  File  filter item is not an array, use it as web/tmp/Master  one item
          //return example.com:  eachObj[eachKey].includes(currentFilterItem)
 scp user@           return currentCandidate === $val  currentFilterItem
        }
      });
  left hand  
      return result;
    });
  }
  
  right side val  goodresult = nestedFilter(cars, query);
 data //commnets   console.debug(goodresult)
2

Answers 3 : of Filtering an array by multiple property values

You can check if the value of the round table "filterKey" is not an array, make it an double chance array, and check if an array has a novel prc subArray

function hasSubArray(master, sub) {
  //coment  return sub.every((i => v => i = !node  master.indexOf(v, i) + $mytext  1)(0));
}

function nlt means  nestedFilter(targetArray, filters) {
  umv val  var filterKeys = Object.keys(filters);
  sort val  return targetArray.filter(function shorthand  (eachObj) {
    return hotkey  filterKeys.every(function (eachKey) {
   more update     var subArray = filters[eachKey];
     valueable   if (!Array.isArray(filters[eachKey])) catch  {
        subArray = tryit  [filters[eachKey]];
      }

      do it  return hasSubArray(eachObj[eachKey], while  subArray);
    });
  });
}

Top rated topics

Why i'm able to print BARCODE EAN13 but not BARCODE ITF using bytes on my Android/JAVA app?

Nestjs-mongoose One-To-One "relation" like / populate not working

Add class to an input when its focused, and remove it when isnt focused

Login button to check DB

Is this a flaw in passing arguments by reference in C++

Sampling from static data set to create dataframe, ignore index in Python

JavaScript conditional font color change of part of text

How to read an array inside another array in an " Json object" -> Jade/PUG.js

How to compare left and right directional content of randomly generated vectors

How to call a function by its name (will be changed by user input) in C++?

Why doesn't cout work in my code and doesn't show (i - 1) to me?

Kmeans clustering with map reduce in spark

Kivy and infinite thread loop - app freezes

Problem when trying to import data from a django directory

Angular Testing Circular Dependency Error on Service in Component

How should I use strcmp() to check whether a string is found in a larger string?

Expo app is not running on the device eventhough connected on the same wifi network

Can the player have a heavier weight?

_splashBloc..add(SplashStartEvent() gives error 'owner!._debugCurrentBuildTarget == this': is not true."

Pass URL parameters into the 3rd page of the funnel

Optimal transposition of a rectangular 2D array

How to create empty 3D array (string) and how to update the value for a specific index?

My python innterpreter works weird 5*5 = 28

How do I launch a console app with the same settings as the one that Visual Studio is using?

How can I perform routing according to page click in flutter for below sample code

Why Julia is printing junk data to the file?

Autofac did not intercept which type of IInterceptor

Bootstrap 4.0 Responsive Number of Cards Per Row For Dynamic Website

Executing Python Async functions in Actix web sockets

Pandas: Generate column on group by and cumsum

Java - generate and rotate matrix

Get all links (Invite Links) in a server using Discum Seflbot

Python: How do you decorate methods in child classes using a method in the parent class?

Why does pandas ask for freq or x when doing seasonal decomposition?

How do I center the input in div

R: Paste and combine multiple outputs from ifelse after grepl of patterns in list

Extracting Tables from PDF with merged rows

Getting error CS1955: Non-invocable member 'SceneManager' cannot be used like a method

Why does ArgoCD keep saying that it cannot create my application?

Reading emails using Java on Android

Cannot run mongod using `service mongod start`

Android Preferences defaultValue not works

Flutter web on Firebase Hosting showing 404 page not found

Why does this nodejs program with read stream not exit?

Ruby on Rails 2.3 LTS upgrade issue - undefined method `search' for ["http://rubygems.org/"]:Array (NoMethodError)

Reusable UITableViewController

Problems with libraries in IntelliJ

Thread cancellation before calling join() gives an error

MUI 5 with react-window for Autocomplete component with multiline texts labels

Wifi not connecting on click on list view item

Top