How to omit specific properties from an object in JavaScript

后端 未结 14 1279
无人共我
无人共我 2021-02-01 14:50

Is there a clean way to return a new object that omits certain properties that the original object contains without having to use something like lodash?

14条回答
  •  醉酒成梦
    2021-02-01 15:40

    If you know the list of the properties that you want preserved as well as omitted, the following "whitelisting" approach should work:

    const exampleFilter = ({ keepMe, keepMeToo }) => ({ keepMe, keepMeToo })
    
    console.log(
      exampleFilter({
        keepMe: 'keepMe',
        keepMeToo: 'keepMeToo',
        omitMe: 'omitMe',
        omitMeToo: 'omitMeToo'
      })
    )

提交回复
热议问题