How to omit specific properties from an object in JavaScript

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

    const {omittedPropertyA, omittedPropertyB, ...remainingObject} = originalObject;
    

    Explanation:

    With ES7

    you could use object destructuring and the spread operator to omit properties from a javascript object:

    const animalObject = { 'bear': 1, 'snake': '2', 'cow': 3 };
     
    const {bear, ...other} = animalObject
    
    // other is: { 'snake': '2', 'cow:'  3}
    

    source: https://remotedevdaily.com/two-ways-to-omit-properties-from-a-javascript-object/

提交回复
热议问题