Check equality of an objects array property and a nested object property On my app I'm checking the equality between an objects array property and a nested object property - based on the results I return a new objects array.
The items objects array:
[{name: 'test', value: 1, category: 'a'},{name: 'test2', value: 2, category: 'b'},{name: 'test3', value: 3, category: 'c'}]

The itemList object:
{testnum1: {name: 'test123', category: 'a'}, testnum2: {name: 'test234', category: 'b'}

I need to check the equality between the category properties and return the following objects array:
[{name: 'test', itemCategory: 'testnum1'},{name: 'test2', itemCategory: 'testnum2'}]

This is what I did, any idea on how to make it more readable?
const getCategorizedItems = items =&gt; {
  const itemListCategories = Object.keys(itemList)
  const categorizedItems= items.map(item=&gt; {
    const itemCategory = itemListCategories.find(category =&gt;
      item.category === itemList[category].category 
    )
    if (itemCategory ) return {name: item.name, category: itemCategory }
  })
  .filter(f =&gt; f)
  return categorizedItems
}
Short review;

Unless you are writing inline functions, you should stick to using function
Using semicolons is preferred, it takes skill and knowledge to avoid the pitfalls of not using semicolons
You assign in 1 statement the value of categorizedItems and return it in the next, you might as well return immediately
In essence you do a find within a map, not too fast, I would create a lookup structure before the map
itemList seems like a terrible name, I would call it categories

I would counter-propose the following;


const items = [{name: 'test', value: 1, category: 'a'}, {name: 'test2', value: 2, category: 'b'}, {name: 'test3', value: 3, category: 'c'}];
const categories = {testnum1: {name: 'test123', category: 'a'}, testnum2: {name: 'test234', category: 'b'}};
                    
function getCategorizedItems(items, categories){
  //Desired output; [{name: 'test', itemCategory: 'testnum1'},{name: 'test2', itemCategory: 'testnum2'}]
  const categoriesMap = Object.fromEntries(Object.entries(categories).map((entry) =&gt; [entry[1].category, entry[0]]));
  return items.map(item =&gt; ({name: item.name, itemCategory:categoriesMap[item.category]})).filter(item =&gt; item.itemCategory);
}

console.log(getCategorizedItems(items, categories));