简体   繁体   中英

Copy all but one field with Lodash, without using Object.assign()

If I have an object like so:

const obj  = { A: 1, B: 2, C: 3, D: 4 };

How can I copy all key/values except for C to a new object?

Underscore has the _.pick() functionality, but I am looking to do the opposite.

您可以使用省略方法来实现此目的: https//lodash.com/docs/4.17.4#omit

You can do this with the ES object rest/spread proposal. Since it's a stage 4 proposal , and not supported by all browser , you might need to transpile the code using babel with the Object rest spread transform .

 const obj = { A: 1, B: 2, C: 3, D: 4 }; const { C, ...objWithoutC } = obj; console.log(objWithoutC); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM