简体   繁体   English

javaScript过滤嵌套对象和数组

[英]javaScript filter nested objects and arrays

My use case is something like this. 我使用的情况是这样的。

  1. I have an array that has an object. 我有一个对象数组。
  2. That each object has an array called menu 每个对象具有一个数组称为menu
  3. Again that menu array has objected. 再次该菜单阵列已经反对。
  4. That each object has an array dish_has_categories 每个对象都有一个数组dish_has_categories
  5. In dish_has_categories array, if there is an object with CategoryId is equal to 8 I want to filter out that root object. dish_has_categories数组中,如果存在CategoryId等于8的对象,我想过滤掉该根对象。

My original data object 我的原始数据对象

 const data = [{ menuName: "Hot dogs", menu: [ { dishId: '1', dish_has_categories: [{ CategoryId: '8' }] }, { dishId: '2', dish_has_categories: [{ CategoryId: '9' }] }] }, { menuName: "Burgers", menu: [{ dishId: '3', dish_has_categories: [{ CategoryId: '6' }] }, { dishId: '4', dish_has_categories: [{ CategoryId: '4' }] }] }, { name: "Drinks", menu: [] } ] 

My expect result is 我的预期结果是

 [{ menuName: "Hot dogs", menu: [ { dishId: '1', dish_has_categories: [{ CategoryId: '8' }] }, { dishId: '2', dish_has_categories: [{ CategoryId: '9' }] }] }] 

what I've done up to now is 我到目前为止所做的是

const data2 = data.filter(element => {
    return element.menu.length > 0
})

I have no idea how to deep filter inside nested objects and arrays. 我不知道如何在嵌套对象和数组内部进行深度过滤。 Hope my question is clear to you all. 希望我的问题是清楚你所有。

You can use filter() with nested some() . 您可以使用带有嵌套some() filter() some()

The some() method tests whether at least one element in the array passes the test implemented by the provided function. some()方法测试数组中的至少一个元素是否通过了由提供的函数实现的测试。 It returns a Boolean value 它返回一个布尔值

 const data = [{ menuName: "Hot dogs", menu: [ { dishId: '1', dish_has_categories: [{ CategoryId: '8' }] }, { dishId: '2', dish_has_categories: [{ CategoryId: '9' }] }] }, { menuName: "Burgers", menu: [{ dishId: '3', dish_has_categories: [{ CategoryId: '6' }] }, { dishId: '4', dish_has_categories: [{ CategoryId: '4' }] }] }, { name: "Drinks", menu: [] } ] const res = data.filter(x => x.menu.some(y => y.dish_has_categories.some(z => z.CategoryId === '8') ) ); console.log(res) 

You can use filter and some 您可以使用过滤器一些

Here nested some is used to check whether any of dish_has_categories has CategoryId equal to '8' , if it is true then we include that menu in final output else we don't 这里嵌套一些用来检查是否有任何dish_has_categories已经CategoryId等于'8' ,如果是true那么我们包括在最终输出菜单,否则我们不

 const data =[{ menuName: "Hot dogs", menu: [ { dishId: '1', dish_has_categories: [{ CategoryId: '8' }] }, { dishId: '2', dish_has_categories: [{ CategoryId: '9' }] }] }, { menuName: "Burgers", menu: [{ dishId: '3', dish_has_categories: [{ CategoryId: '6' }] }, { dishId: '4', dish_has_categories: [{ CategoryId: '4' }] }] }, { name: "Drinks", menu: [] } ] let op = data.filter(val => { let menu = val.menu.some(({dish_has_categories}) => dish_has_categories.some(({CategoryId}) => CategoryId === '8')) return menu }) console.log('filtered values -->\\n',op) let names = op.map(({menuName})=> menuName) console.log('Names --> \\n', names) 

You can use javascript filter() and some() . 您可以使用javascript filter()some() some() method checks if any of the elements in an array pass the function. some()方法检查数组中的任何元素是否通过该函数。

let data2 = data.filter(element => {
    let menu = element.menu.some(({dish_has_categories}) => dish_has_categories.some(({CategoryId}) => CategoryId === '8'));
    return menu;
           });
    console.log(data2); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM