Skip to content

Javascript Filter and Some on Array of Objects

I have this array of objects

 appLearningItemId: 67
 catalogues: (2) [ {id: 1041, value: "New Catalog"},
                   {id: 1058, value: "Test"}]
 categories: (3) [{id: 1, value: "Soft Skills"},
                  {id: 3, value: "Non-technical"},
                  {id: 5, value: "Induction"}]
 code: "CCE0013"
 suppliers: (3) [{id: 1, value: "Company XYZ Ltd"},
                 {id: 2, value: "test c2"},
                 {id: 17, value: "new company"} ]
 title: "07 Values & Beliefs"
 type: {id: 11377, value: "Elearning"}
 }, ... * 682 items

I have to filter this list with 4 different select boxes, Catalogues, Categories, Suppliers, Type.

I can filter the list for type because it is not in an array like so

const typeList = this.originalLearningItems.filter(item => item.type.value === val.typeSearch)

but for catalogues, categories and suppliers they are a level down. I have tried

 const listHasCatalogs = this.originalLearningItems.filter(item =>
            catalogs => catalogs.some(catalog => catalog.value == val.catalogSearch)

but I get catalogs.some is not an object, I also tried catalogs.filter but I get the same error.

What is the best way to filter the items based on values within a nested array of objects?


.some is a function that belongs to Arrays. You are calling it on each element of the array.

Try this instead.

const listHasCatalogs = this.originalLearningItems
    .filter(item => 
        item.catalogues.some(catalog => catalog.value == val.catalogSearch)