1

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.

3
  • 3
    That shouldn’t be working, since you said categories was a plain object. Those don’t have filter. Is categories actually an array? Commented Dec 1, 2018 at 14:56
  • It shouldn't be working another way: _id doesn't match the data either which has id. Commented Dec 1, 2018 at 15:04
  • 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" Commented Dec 1, 2018 at 15:21

4 Answers 4

1

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(cat.id);
});

console.log(categoriesFilter);

Sign up to request clarification or add additional context in comments.

5 Comments

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.
0

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, {[v.id]: {[k]: v}}), {})

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

console.log(result)

Comments

0

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(catValue.id); });

1 Comment

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.
0
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.id) ? cat : 0));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.