Filter an Object based on values in array

javascript filter array of objects by key
javascript filter array multiple values
javascript filter array of objects by another array of objects
array filter
javascript array.filter multiple arguments
javascript filter object
javascript filter array of objects by array of strings
es6 filter array of objects

I encountered a small problem, that I can't figure out or find a solution to, on the internet, at least that I'm aware of. I want to filter an object in javascript, based on a undefined number of values in an array. I have an object like this:

categories = { "category1: {"_id": "1234"}, "category2: {"_id": "4567"}, ... };

I also have an array including values of IDs, like so:

catArray = ["1234", "4567", ... ]

Now I want to filter all categories out of the category-Object, matching the IDs from the array. I managed to filter the category for a single array-value, but not for all. It's working like this:

const categoriesFilter = categories.filter(cat => {
        return cat._id == catArray[0];

So far it is working. But now I want to match the categories from all possible ID-values from the array. I tried to do it with a for-loop, but it's not working out. Any idea? Thanks a lot in advance.

Categories should be an array of objects, for example. Then you can use .filter():

let categories = [{ "id": "1234" }, { "id": "4567" }];
let catArray = ["1234", "45674"];

const categoriesFilter = categories.filter(cat => {
  return catArray.includes(;


JavaScript Array Filter: Filtering Array Elements Based on a Test , Suppose you have an array of city objects where each object contains two properties: name and population . let cities = [ {name: 'Los Angeles', population:  The filter () method includes only the element in the result array if the element satisfies the test in the function that we pass into. In ES6, it is even cleaner when you use the arrow function ( => ). let bigCities = cities.filter ( city => city.population > 3000000 ); console .log (bigCities);

You could first create one object with id as keys and then use it as lookup table to create new object with filtered properties by array.

const cat = { category1: {"id": "1234"}, category2: {"id": "4567"}, category3: {"id": "123"}};
const arr = ["1234", "4567" ]
const hash = Object.entries(cat)
  .reduce((r, [k, v]) => Object.assign(r, {[]: {[k]: v}}), {})

const result = arr.reduce((r, id) => {
  if(hash[id]) Object.assign(r, hash[id])
  return r;
}, {})


filter() Array Method in JavaScript ←, filter() creates a new array with elements that fall under a given criteria from an existing array: This is useful for accessing properties, in the case of objects. You could use array's filter() function: function filter_dates(event) { return == "22-02-2016"; } var filtered = events.filter(filter_dates); The filter_dates() method can be standalone as in this example to be reused, or it could be inlined as an anonymous method - totally your choice =]

You may do :

var categories = { "category1": {"id": "1234"}, "category2": {"id": "4567"}, "category4": {"id": "45678"}}; var catArray = ["1234", "4567"]; var categoriesFilter = Object.entries(categories).filter(([catKey,catValue]) => { return catArray.includes(; });

Array.prototype.filter(), values(item)); return !item.string.includes("Lets"); });. The Object.values method gives an array, so we can filter that, but  Filtering out an array of objects based on an array of values in a react component: const filteredResults = result => !this.state.filterOut.includes(result.category) ) where in an array of objects and this.state.filterOut is an array of values that correspond the 'category' key in the objects that I wanted to remove.

const categories = { category1: {"id": "1234"}, category2: {"id": "4567"}, category3: {"id": "123"}};
const catArray = ["1234", "4567" ]
const checkIfArrIncludes = (id) => catArray.includes(id);
const matchedCats = Object.values(categories).filter(cat => (checkIfArrIncludes( ? cat : 0));

Filter array of object - JavaScript, The filter() method returns an array containing elements of the parent array that The .filter() method can be used to filter an array of objects which share a  Raw. * Filter an array of objects. * You can pass in one or more properties on which to filter. * level of node. * ofilter ($items, ['size' => 3, 'name']); // filter anything that has the size property === 3 and a 'name' property with value.

Array .filter() Method ―, You can search in the whole array for a value like this: var young = people.filter((​obj) => { var flag = false; Object.values(obj).forEach((val) => { if(String(val). This time instead of piping objects to the Where-Object command, I'm going to create an array and then do my filtering. This essentially performs the same task but in a different way. This essentially performs the same task but in a different way.

JavaScript, Note: filter() does not execute the function for array elements without values. Note​: filter() does arr, Optional. The array object the current element belongs to  I use my ruleOut function for filtering objects based on specific unwanted property values. I understand that in your example you would like to use conditions instead of values, but my answer is valid for the question title, so I'd like to leave my method here.

JavaScript Array filter() Method, then it must return a boolean value, which will determine if the item Filters an array of objects (one level-depth) with multiple criteria. Supporting array and also object as the filter criteria not only increase the base-code but also the current  The filter() method creates an array filled with all array elements that pass a test (provided as a function). Note: filter() does not execute the function for array elements without values. Note: filter() does not change the original array.

  • That shouldn’t be working, since you said categories was a plain object. Those don’t have filter. Is categories actually an array?
  • It shouldn't be working another way: _id doesn't match the data either which has id.
  • Maybe i switched the category. I got it from some other code and it is working, i just put that object above as an example. And the ID was a mistake. It should be "_id"
  • That isn't a valid array.
  • It isn't, you're supposed to fill the ... with other elements.
  • No. You still have object keys in there. category1 for example.
  • You're right. Sorry, didn't pay attention. Edited the answer.
  • Just added a snippet to your answer.
  • that worked half way. I can iterate now trough the IDs from the array. But all the other keys and values from the categories objects got lost.