神刀安全网

Object.keys

I adore JavaScript objects.  Love them.  You’re probably asking "well, why don’t you marry them?"  Trust me:  if I could, I would.  Arrays are nice and all but object keys provide another level of structure and information that is invaluable.  For example, it’s much faster search an object for a key than it is to search an array for value presence.

The way we’ve always iterated on an Object instance was always a for loops with a hasOwnProperty check which was ugly; Object.keys (not Object.prototype.keys ) provides an array of Object properties!

var person = {   firstName: 'David',   lastName: 'Walsh',   // ... };  Object.keys(person).forEach(function(trait) {   console.log('Person ', trait,': ', person[trait]); });

If you work with JSON or simply raw JavaScript objects, and you haven’t been using Object.keys , now is the time to ditch the old method for this elegant solution!

Object.keys Object.keys Object.keys Object.keys Object.keys

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » Object.keys

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
分享按钮