Remove value from object without mutation

后端 未结 8 796
天涯浪人
天涯浪人 2020-12-07 18:50

What\'s a good and short way to remove a value from an object at a specific key without mutating the original object?

I\'d like to do something like:



        
8条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-07 19:22

    with lodash cloneDeep and delete

    (note: lodash clone can be used instead for shallow objects)

    const obj = {a: 1, b: 2, c: 3}
    const unwantedKey = 'a'
    
    const _ = require('lodash')
    const objCopy = _.cloneDeep(obj)
    delete objCopy[unwantedKey]
    // objCopy = {b: 2, c: 3}
    

提交回复
热议问题