The World’s Largest Online Community for Developers

'; javascript - How to filter an array by properties inside an array of the array - LavOzs.Com

I'm trying to filter every establishment in the establishments array by whether specialCategories is found in the array establishmentSpecials of the establishments array itself.

I'm able to filter for the establishmentSpecials array for every establishment but I want to keep the establishment portion, as evident by the image.

            //masterEstablishments array is from the db

            //The categories array, to filter out all but the following
            specialCategories = ["Breakfast Special"];

            //The array to hold the filtered establishments
            var establishments;

            //For all Establishments
            for(var i=0;i<masterEstablishments.length;i++){
                //Filter out Catorgory from establishmentSpecials where Category is not found in specialCategories
                establishments = masterEstablishments[i].establishmentSpecials.filter(x => specialCategories.includes(x.Category));

            console.log("Filtered establishments");

In summary, for example

        var establishments = [ {address: "55 Garden rd"},
            [{Category: "Breakfast Special"}, {Category: "Dinner Special"}]

Should become

        establishments = [ {address: "55 Garden rd"},
            [{Category: "Breakfast Special"}]

since "Dinner Special" is not in specialCategories

specialCategory = [{Category: "Breakfast Special"}]

Many thanks.

enter image description here

You should be filtering the main array, not the establishmentSpecials property. Use array.some() to check whether any of the specials is in the special categories array.

establishments = masterEstablishments.filter(e => e.establishmentSpecials.some(x => specialCategories.includes(x.Category)))

Do you mean you just wanted to remove {Category: "Dinner Special"} on the Establishments?

        specialCategories = ["Breakfast Special"];

        const establishments = => {
          return {
            Cuisine_Type: element.Cuisine_type,
            Address: element.Address, // add the other properties so on and so forth
            establishmentSpecials : =>{
              if (ES_element.Category === specialCategories[0]) {
                return ES_element;
How do JavaScript closures work?
How do I check if an element is hidden in jQuery?
How do I remove a property from a JavaScript object?
How do I check if an array includes a value in JavaScript?
How to append something to an array?
How do I redirect to another webpage?
Sort array of objects by string property value
How to check if an object is an array?
How can I remove a specific item from an array?
For-each over an array in JavaScript