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

后端 未结 27 2058
刺人心
刺人心 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:37

    To add another esoteric way, this works aswell:

    var obj = {a: 1, b:2, c:3}
    var newobj = {a,c}=obj && {a,c}
    // {a: 1, c:3}
    

    but you have to write the prop names twice.

提交回复
热议问题