简体   繁体   English

如何在es6中过滤嵌套对象并返回父对象

[英]how to filter nested object in es6 and return the parent

I have an object that contains nested objects. 我有一个包含嵌套对象的对象。 i would like to filter through them and return the key if present. 我想通过它们过滤并返回密钥(如果存在)。

for example: 例如:

var meals = {
    food_meals: [
      {meal_id: 15749, address_required: false, button_text: "choose", can_choose_meal: true},
      {meal_id: 15750, address_required: false, button_text: "choose", can_choose_meal: true}
    ],
    wine_meals: [
      {meal_id: 11651, address_required: false, button_text: "choose", can_choose_meal: true},
      {meal_id: 4424, address_required: false, button_text: "choose", can_choose_meal: true}
    ],
    kids_meals: [
      {meal_id: 15763, address_required: false, button_text: "choose", can_choose_meal: true},
      {meal_id: 15764, address_required: false, button_text: "choose", can_choose_meal: true},
      {meal_id: 15765, address_required: false, button_text: "choose", can_choose_meal: true}
    ]
}

If i had a meal with meal_id of 15764 then I would want the key of that value returned (in this case kids_meals ) 如果我有一顿饭,其饭餐meal_id为15764,那么我希望返回该值的键(在本例中为kids_meals

i can filter the meal from the nested object by doing 我可以通过执行以下操作从嵌套对象中过滤餐点

meals.kids_meals.filter(meal => meal.meal_id == this.props.selection.meal_id)

where this.props.selection.meal_id is 15764 其中this.props.selection.meal_id为15764

my desired output is 'kids_meals' in this case, but i can't seem to reach it 在这种情况下,我想要的输出是“ kids_meals”,但我似乎无法达到它

You can iterate over the Object.entries of the meals to get an array of key-value pairs, and use .find on that array to get the key-value pair whose value is an array that contains the matching meal_id : 您可以遍历mealsObject.entries以获取键值对数组,并在该数组上使用.find来获取键值对,其值是包含匹配的meal_id的数组:

 const meals = { food_meals: [ {meal_id: 15749, address_required: false, button_text: "choose", can_choose_meal: true}, {meal_id: 15750, address_required: false, button_text: "choose", can_choose_meal: true} ], wine_meals: [ {meal_id: 11651, address_required: false, button_text: "choose", can_choose_meal: true}, {meal_id: 4424, address_required: false, button_text: "choose", can_choose_meal: true} ], kids_meals: [ {meal_id: 15763, address_required: false, button_text: "choose", can_choose_meal: true}, {meal_id: 15764, address_required: false, button_text: "choose", can_choose_meal: true}, {meal_id: 15765, address_required: false, button_text: "choose", can_choose_meal: true} ] }; const idToFind = 15764; const foundEntry = Object.entries(meals) .find( ([, arr]) => arr.some( ({ meal_id }) => meal_id === idToFind ) ); if (foundEntry) { console.log(foundEntry[0]); } 

The function filter is for creating a new array with objects which meet a specific condition. 函数filter用于创建具有满足特定条件的对象的新数组。 In this case, you need to use the function find to extract an object which meets a condition. 在这种情况下,您需要使用函数find来提取满足条件的对象。

This alternative uses the function find along with the function some to find at least one object with a specific meal_id . 此替代方法使用功能findsome功能来查找至少一个具有特定meal_id对象。

 let meals = { food_meals: [ {meal_id: 15749, address_required: false, button_text: "choose", can_choose_meal: true}, {meal_id: 15750, address_required: false, button_text: "choose", can_choose_meal: true} ], wine_meals: [ {meal_id: 11651, address_required: false, button_text: "choose", can_choose_meal: true}, {meal_id: 4424, address_required: false, button_text: "choose", can_choose_meal: true} ], kids_meals: [ {meal_id: 15763, address_required: false, button_text: "choose", can_choose_meal: true}, {meal_id: 15764, address_required: false, button_text: "choose", can_choose_meal: true}, {meal_id: 15765, address_required: false, button_text: "choose", can_choose_meal: true} ]}, target = 15764, found = Object.keys(meals).find(k => meals[k].some(({meal_id}) => meal_id === target)); console.log(found); 

Using Array#filter , Array#findIndex and Object.keys 使用Array#filter,Array#findIndex和Object.keys

 const data={food_meals:[{meal_id:15749,address_required:!1,button_text:"choose",can_choose_meal:!0},{meal_id:15750,address_required:!1,button_text:"choose",can_choose_meal:!0}],wine_meals:[{meal_id:11651,address_required:!1,button_text:"choose",can_choose_meal:!0},{meal_id:4424,address_required:!1,button_text:"choose",can_choose_meal:!0}],kids_meals:[{meal_id:15763,address_required:!1,button_text:"choose",can_choose_meal:!0},{meal_id:15764,address_required:!1,button_text:"choose",can_choose_meal:!0},{meal_id:15765,address_required:!1,button_text:"choose",can_choose_meal:!0}]} const id = 15764; const res = Object.keys(data).filter(key=>{ return data[key].findIndex(({meal_id})=>meal_id===id) > -1 }).join(""); console.log(res); 

I realize this is relatively close to @CertainPerformance 's answer, and I don't mean to say that his is invalid at all, however, you might find that this is a bit easier to read, and secondly if there's ever a meal that is available in more than one category( a use case that you may not have thought of ), it'll return an array. 我意识到这与@CertainPerformance的答案相对接近,我并不是要说他根本是无效的,但是,您可能会发现这更容易阅读,其次,如果有餐可用于多个类别(您可能没有想到的用例)中,它将返回一个数组。

Object.keys(meals).filter(type => meals[type].some(item => item.meal_id === idToFind));

 var meals = { food_meals: [ {meal_id: 15749, address_required: false, button_text: "choose", can_choose_meal: true}, {meal_id: 15750, address_required: false, button_text: "choose", can_choose_meal: true} ], wine_meals: [ {meal_id: 11651, address_required: false, button_text: "choose", can_choose_meal: true}, {meal_id: 4424, address_required: false, button_text: "choose", can_choose_meal: true} ], kids_meals: [ {meal_id: 15763, address_required: false, button_text: "choose", can_choose_meal: true}, {meal_id: 15764, address_required: false, button_text: "choose", can_choose_meal: true}, {meal_id: 15765, address_required: false, button_text: "choose", can_choose_meal: true} ] } let r = Object.keys(meals).filter(type => meals[type].some(item => item.meal_id === 15749)); console.log(r); 

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

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