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

时间:2019-01-10 00:49:47

标签: javascript reactjs ecmascript-6

我有一个包含嵌套对象的对象。我想过滤掉它们并返回密钥(如果存在)。

例如:

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}
    ]
}

如果我吃的meal_id饭菜为15764,那么我希望返回该值的键(在这种情况下为kids_meals

我可以通过

从嵌套对象中过滤餐点
meals.kids_meals.filter(meal => meal.meal_id == this.props.selection.meal_id)

其中this.props.selection.meal_id是15764

在这种情况下,我想要的输出是'kids_meals',但我似乎无法达到

4 个答案:

答案 0 :(得分:5)

您可以遍历Object.entries的{​​{1}}以获取键值对数组,并在该数组上使用meals以获取值为包含匹配的.find的数组:

meal_id

答案 1 :(得分:0)

函数filter用于创建带有满足特定条件的对象的新数组。在这种情况下,您需要使用函数find来提取满足条件的对象。

此替代方法使用函数find和函数some来查找至少一个具有特定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);

答案 2 :(得分:0)

使用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);

答案 3 :(得分:0)

我意识到这与@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);