Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Say I have an object:(说我有一个对象:)

elmo = { color: 'red', annoying: true, height: 'unknown', meta: { one: '1', two: '2'} }; I want to make a new object with a subset of its properties.(我想用其属性的子集创建一个新对象。) // pseudo code subset = elmo.slice('color', 'height') //=> { color: 'red', height: 'unknown' } How may I achieve this?(我该如何实现?)   ask by Christian Schlensker translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
371 views
Welcome To Ask or Share your Answers For Others

1 Answer

Using Object Destructuring and Property Shorthand(使用对象分解和属性速记)

const object = { a: 5, b: 6, c: 7 }; const picked = (({ a, c }) => ({ a, c }))(object); console.log(picked); // { a: 5, c: 7 } From Philipp Kewisch:(来自Philipp Kewisch:) This is really just an anonymous function being called instantly.(这实际上只是一个即时调用的匿名函数。) All of this can be found on the Destructuring Assignment page on MDN.(所有这些都可以在MDN的“ 解构分配”页面上找到。) Here is an expanded form(这是扩展形式) let unwrap = ({a, c}) => ({a, c}); let unwrap2 = function({a, c}) { return { a, c }; }; let picked = unwrap({ a: 5, b: 6, c: 7 }); let picked2 = unwrap2({a: 5, b: 6, c: 7}) console.log(picked) console.log(picked2)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...