JavaScript filter deeply

时间:2019-05-30 07:13:04

标签: javascript arrays

I've use case something like this,

  1. I have an array it has objects.
  2. That each object represents a restaurant menu
  3. Each restaurant menu object has an array called dishes
  4. Each dish object has taggedCategory array.
  5. Again in that array has objects.
  6. From that objects I need to filter where dish_category_master.id = 13 and return the root object with filtered dish objects.

This is my original data object.

const data = [{
        menuName: "Hot dogs",
        dishes: [{
                dishId: '1',
                taggedCategory: [{
                        dish_category_master: {
                            name: 'Vegetarian',
                            id: '13'
                        }
                    },
                    {
                        dish_category_master: {
                            name: 'Non Vegetarian',
                            id: '14'
                        }
                    },
                ]
            },
            {
                dishId: '2',
                taggedCategory: [{
                        dish_category_master: {
                            name: 'Non Vegetarian',
                            id: '14'
                        }
                    },
                    {
                        dish_category_master: {
                            name: 'Healthy',
                            id: '15'
                        }
                    },
                ]
            },
            {
                dishId: '3',
                taggedCategory: [{
                    dish_category_master: {
                        name: 'Fast food',
                        id: '18'
                    }
                }, ]
            }
        ]

    },
    {
        menuName: "Drinks",
        dishes: []
    },
    {
        menuName: "Burgers",
        dishes: [{
                dishId: '4',
                taggedCategory: []

            },
            {
                dishId: '5',
                taggedCategory: [{
                    dish_category_master: {
                        name: 'Vegetarian',
                        id: '13'
                    }
                }]
            },
        ]
    },



]

This is what I expect after filtration.

const data = [{
        menuName: "Hot dogs",
        dishes: [{
            dishId: '1',
            taggedCategory: [{
                    dish_category_master: {
                        name: 'Vegetarian',
                        id: '13'
                    }
                },
                {
                    dish_category_master: {
                        name: 'Non Vegetarian',
                        id: '14'
                    }
                },
            ]
        }]

    },
    {
        menuName: "Burgers",
        dishes: [{
            dishId: '5',
            taggedCategory: [{
                dish_category_master: {
                    name: 'Vegetarian',
                    id: '13'
                }
            }]
        }, ]
    },



]

Only DishId 1 and 5 came because that two are have dish_category_master.id 13

How do I achieve this using JavaScript. Anyhelp! Thanks in advanced.

2 个答案:

答案 0 :(得分:1)

您可以对内部数组的过滤对象进行归约处理,然后检查是否需要id

var data = [{ menuName: "Hot dogs", dishes: [{ dishId: '1', taggedCategory: [{ dish_category_master: { name: 'Vegetarian', id: '13' } }, { dish_category_master: { name: 'Non Vegetarian', id: '14' } }] }, { dishId: '2', taggedCategory: [{ dish_category_master: { name: 'Non Vegetarian', id: '14' } }, { dish_category_master: { name: 'Healthy', id: '15' } }] }, { dishId: '3', taggedCategory: [{ dish_category_master: { name: 'Fast food', id: '18' } }] }] }, { menuName: "Drinks", dishes: [] }, { menuName: "Burgers", dishes: [{ dishId: '4', taggedCategory: [] }, { dishId: '5', taggedCategory: [{ dish_category_master: { name: 'Vegetarian', id: '13' } }] }] }],
    target = '13',
    result = data.reduce((r, { dishes = [] }) =>
        r.concat(dishes.filter(({ taggedCategory = [] }) =>
            taggedCategory.some(({ dish_category_master: { id } }) => id === target)
        )),
        []
    );

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:0)

使用Object destructuringArray#prototype#mapArray#prototype#filter的组合基于搜索ID 13进行过滤。

const data = [
		{
        menuName: "Hot dogs",
        dishes: [{
                dishId: '1',
                taggedCategory: [
					{
                        dish_category_master: {
                            name: 'Vegetarian',
                            id: '13'
                        }
                    },
                    {
                        dish_category_master: {
                            name: 'Non Vegetarian',
                            id: '14'
                        }
                    },
                ]
            },
            {
                dishId: '2',
                taggedCategory: [{
                        dish_category_master: {
                            name: 'Non Vegetarian',
                            id: '14'
                        }
                    },
                    {
                        dish_category_master: {
                            name: 'Healthy',
                            id: '15'
                        }
                    },
                ]
            },
            {
                dishId: '3',
                taggedCategory: [{
                    dish_category_master: {
                        name: 'Fast food',
                        id: '18'
                    }
                }, ]
            }
        ]

    },
    {
        menuName: "Drinks",
        dishes: []
    },
    {
        menuName: "Burgers",
        dishes: [{
                dishId: '4',
                taggedCategory: []

            },
            {
                dishId: '5',
                taggedCategory: [{
                    dish_category_master: {
                        name: 'Vegetarian',
                        id: '13'
                    }
                }]
            },
        ]
    }
];

const searchId = '13';

const res = data.map(x => {
	// Destructure the dishes and rest properties
	const { dishes, ...rest } = x;
	
	// Filter the dishes which matches the searchId '13' and later filter the empty taggedCategory arrays
	const foundDishes = dishes.map(y => {
		const { taggedCategory, ...restDishes } = y;
		let filteredTaggedCategories;
		if (taggedCategory.some(z => z.dish_category_master.id === searchId)) {
			filteredTaggedCategories = taggedCategory;
		}		 
		return { ...restDishes, taggedCategory: filteredTaggedCategories };
	})
	.filter(a => a && a.taggedCategory && a.taggedCategory.length);
	
	return { ...rest, dishes: foundDishes };
})
.filter(x => x.dishes && x.dishes.length);

console.log(res);