I've use case something like this,
array
called dishes
taggedCategory
array.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.
答案 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 destructuring
,Array#prototype#map
和Array#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);