Filter array of objects by multiple properties and values

filter array of objects by property
javascript array.filter multiple arguments
javascript filter array of objects with multiple conditions
javascript array.filter multiple conditions
filters an array of objects by custom predicates
javascript filter array of objects by key
angular 4 filter array multiple values
swift 4 filter array of objects

Is it possible to filter an array of objects by multiple values?

E.g in the sample below can I filter it by the term_ids 5 and 6 and type car at the same time?

[  
   {  
      "id":1,
      "term_id":5,
      "type":"car"
   },
   {  
      "id":2,
      "term_id":3,
      "type":"bike"
   },
   {  
      "id":3,
      "term_id":6,
      "type":"car"
   }
]

Definitely up for using a library if it makes it easier.

You can do it with Array.filter

var data = [{
    "id": 1,
    "term_id": 5,
    "type": "car"
  },
  {
    "id": 2,
    "term_id": 3,
    "type": "bike"
  },
  {
    "id": 3,
    "term_id": 6,
    "type": "car"
  }
];

var result = data.filter(function(v, i) {
  return ((v["term_id"] == 5 || v["term_id"] == 6) && v.type == "car");
})

console.log(result)

javascript filter array multiple conditions, NOTE: The FILTER method can take an additional this argument, then using an E6 arrow function getElementById('result'); function createMarkUp(data){ Object.keys(query). toUpperCase() + ': ' + result[0][key])); element. (item: T): boolean => item[property] === value } function map<T>(mapper: (key: string, value: any,� Another suggestion caught my attention was to allow filter by range, e.g. details.capacities.fuel > 30 && details.capacities.fuel < 50. Supporting array and also object as the filter criteria not only increase the base-code but also the current approach does not allow filter by ranges, so I decided to simplify the code and delegate the search criteria to the object that describe the filters

You can do this with plain js filter() method and use && to test for both conditions.

var data = [{"id":1,"term_id":5,"type":"car"},{"id":2,"term_id":3,"type":"bike"},{"id":3,"term_id":6,"type":"car"}];

var result = data.filter(function(e) {
  return [5, 6].includes(e.term_id) && e.type == 'car'
});

console.log(result);

Filters an array of objects with multiple match-criteria. � GitHub, Where the `filters` argument is an object that contains a `key: string`. * and its value is a function with the value of the property to evaluate. * As the function� * Filter an array of objects. * You can pass in one or more properties on which to filter. * If the key of an array is an array, then it will filtered down to that

The following function will help you out.

    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]);
           });
       });
    };

Use this function with filters described as below:

var filters = {
    "id": ["3"],
    "term_id": ["6"],
    "type": ["car","bike"]
}

Dont pass empty array. If there are no values in the array, skip that property in the filters.

The result will be filtered array.

Filtering an array of JSON objects by multiple properties : javascript, Array filter? Array map? Array reduce? None of these? I was looping the objects and setting the visible property of the object to either true or false. The filter() method creates a new array with all the elements that pass the test implemented by the callback() function. Internally, the filter() method iterates over each element of the array and pass each element to the callback function. If the callback function returns true, it includes the element in the return array.

Filter an object by multiple properties - Programming Help, I'm trying to do a fuzzy search, which I've partially achieved on the slug and title fields it's just this value field in the headings array that's driving� /** * Filter all objects based on Type and Color filters. * The type is always a single type, or an empty string. * The colors are an array of one or multiple colors. * TODO: Implement URL writing for multiple colors, e.g. /colors/red+blue * @param {Array} arrObjects The list of objects to filter.

JavaScript Array Filter: Filtering Array Elements Based on a Test , In this example, we called the filter() method of the cities array object and passed The filter() method includes only the element in the result array if the element satisfies and in the range specified by the lower and upper properties of an object. Passing By Value � Returning Multiple Values � Function Type � The call ()� I'm wondering what is the cleanest way, better way to filter an array of objects depending on a string keyword. The search has to be made in any properties of the object. When I type lea I want to go trough all the objects and all their properties to return the objects that contain lea

Dynamically filtering an array of objects in JavaScript, I recently needed a way to filter an array of objects based on user input. down filter where the user can select multiple values across multiple columns). properties (the field columns) and filter values would be controlled by� I want to simplify an array of objects. Let's assume that I have following array: var users = [{ name: 'John', email: 'johnson@mail.com', age: 25, address: 'USA' }, {

Comments
  • Thank you! Someone else posted pretty much the same answer at the same time! :)
  • Nice! I was trying filter but didn't know it was possible do it like this! Thank you!
  • @Nenad Vracar, very nice! Is it possible replace searched term without loop through objects? I wanna highlight results.