How to get a subset of a javascript object's properties

后端 未结 27 1664
刺人心
刺人心 2020-11-21 22:46

Say I have an object:

elmo = { 
  color: \'red\',
  annoying: true,
  height: \'unknown\',
  meta: { one: \'1\', two: \'2\'}
};

I want to m

27条回答
  •  日久生厌
    2020-11-21 23:24

    I've got the same problem and solved it easily by using the following libs:

    object.pick

    https://www.npmjs.com/package/object.pick

    pick({a: 'a', b: 'b', c: 'c'}, ['a', 'b'])
    //=> {a: 'a', b: 'b'}
    

    object.omit

    https://www.npmjs.com/package/object.omit

    omit({a: 'a', b: 'b', c: 'c'}, ['a', 'c'])
    //=> { b: 'b' }
    

提交回复
热议问题