最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Copy all but one field with Lodash, without using Object.assign() - Stack Overflow

programmeradmin2浏览0评论

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.

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.

Share Improve this question edited Jun 6, 2017 at 18:36 Alexander Mills asked Jun 6, 2017 at 16:36 Alexander MillsAlexander Mills 100k165 gold badges531 silver badges908 bronze badges 3
  • possible duplicate stackoverflow.com/questions/34698905/… – Dinesh undefined Commented Jun 6, 2017 at 16:48
  • Possible duplicate of clone a js object except for one key – Dinesh undefined Commented Jun 6, 2017 at 16:48
  • thanks for editing the question, much better now – Alexander Mills Commented Jun 6, 2017 at 18:20
Add a comment  | 

2 Answers 2

Reset to default 15

You can achieve this with the omit method : 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);

发布评论

评论列表(0)

  1. 暂无评论