Keeping only certain properties in a JavaScript object

后端 未结 11 1008
时光说笑
时光说笑 2020-12-20 11:05

I have an object. I would like to modify the object (not clone it) by removing all properties except for certain specific properties. For instance, if I started with this o

11条回答
  •  旧时难觅i
    2020-12-20 11:41

    This was the first hit when googling 'js keep only certain keys' so might be worth an update.

    The most 'elegant' solution might just be using underscore.js

    _.pick(myObj, 'p1', 'p2', 'p100')
    

提交回复
热议问题