How to omit specific properties from an object in JavaScript

后端 未结 14 1428
无人共我
无人共我 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:42

    There's the blacklist package on npm which has a very flexible api.

    Also a situational trick using the object rest-spread proposal (stage-3).

    const {a, b, ...rest} = {a: 1, b: 2, c: 3, d: 4};
    a // 1
    b // 2
    rest // {c: 3, d: 4}
    

    This is often used in react components where you want to use a few properties and pass the rest as props to a

    or similar.

提交回复
热议问题