javascript - 如果对象值在javascript中为真,则迭代数组提取并添加到新列表

标签 javascript reactjs react-native

我有一个像下面这样的数组,如果选定的对象值为真,我想提取对象和子数组并创建一个新数组

menu: [
  {
    category_name: "snacks",
    selected: true
    items: [
      {
        item_name: "burger",
        price: 00,
        bestseller: false
      }
    ]
  },
  {
    category_name: "tiffins",
    selected: false
    items: [
      {
        item_name: "idly",
        price: 00,
        bestseller: false
      }
    ]
  },
]

我希望新数组是这样的

new_menu: [
  {
    category_name: "snacks",
    items: [
      {
        item_name: "burger",
        price: 00,
        bestseller: false
      }
    ]
  },
]

最佳答案

你可以试试这个:

const new_menu = menu
  // Get the selected items
 .filter(m => m.sellected)
  // Get rid of the `sellected` property
 .map(({ sellected, ...m }) => m)

关于javascript - 如果对象值在javascript中为真,则迭代数组提取并添加到新列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58146268/

相关文章:

javascript - 无法在 JEST 的 IF 语句中读取 undefined object 的属性

html -::选择仅在使用 - Chrome + ReactJs 在检查器中强制重新渲染后才有效

javascript - 需要更新数组中的值但保留原始值以再次更新它

javascript - 在模态弹出窗口内插入图像

Javascript 查找包含 HTML 的字符串中单词的位置

javascript - 伊诺特 : no such file or directory in Gatsby

javascript - 如何打印开关的开/关状态?

react-native - 类型错误 : Cannot read property 'length' of undefined in react-native

react-native - 如何更改 native 基本输入的字体系列?

javascript - 从函数中抛出和捕获异常