为了账号安全,请及时绑定邮箱和手机立即绑定

Ways to iterate through objects

标签:
JavaScript

Relevant basic concepts

Property accessors

let obj = {  a: 'b'};// dot notationconsole.log(obj.a); // 'b'// bracket notationconsole.log(obj['a']); // 'b'// through prototype chainconsole.log(obj.toString, obj.toString === Object.prototype.toString); // ƒ toString() { [native code] } true

Check whether an property exists in object

  • in operator: returns true if the specified property is in the specified object or its prototype chain.

  • hasOwnProperty: returns a boolean indicating whether the object has the specified property as its own property (as opposed to inheriting it).

let obj = {  a: 'b'};console.log('a' in obj); //trueconsole.log('toString' in obj); //trueconsole.log(obj.hasOwnProperty('a')); //trueconsole.log(obj.hasOwnProperty('toString')); //false

Object.defineProperty && Object.defineProperties - defines new or modifies existing properties directly on an object, returning the object.

let obj = {  a: "b"};Object.defineProperties(obj, {  c: {    value: 'd'
  },  e: {    value: 'f'
  }
});console.log(obj); // {a: "b", c: "d", e: "f"}

getter and setter

We can use getters and setters to generate computed property. E.g.

let people = {  firstName: 'michael',  lastName: 'zheng',
  get fullName() {    return `${this.firstName} ${this.lastName}`
  },
  set fullName(val) {
    [this.firstName, this.lastName] = val.split(' ');
  }
}console.log(people.firstName, people.lastName, people.fullName);//"michael", "zheng", "michael zheng"people.fullName = 'hello world';console.log(people.firstName, people.lastName, people.fullName);//"hello", "world", "hello world"

There are three ways to iterate through objects

let student = {  name: "michael"};Object.defineProperties(student, {  age: {    enumerable: false,    value: 18
  },  grade: {    value: 6,    enumerable: true
  },  sex: {    value: "male",    enumerable: false
  }
});Object.prototype.x = "inherited";

In the sample above, we create an object student. student has following properties:

  • name: self enumerable property

  • age: self non-enumerable property

  • grade: self enumerable property

  • sex: self non-enumerable property

as well as a custom property from prototype chain:

  • x: enumerable

for...in iterates over enumerable properties of an object, including prototype chain.

for (let prop in student) {  console.log(prop); //'name', 'grade', 'x'}for (let prop in student) { // self properties only
  if (student.hasOwnProperty(prop)) {    console.log(prop); // 'name', 'grade'
  }
}

Object.keys returns an array of a given object's property names, only iterate through self enumerable properties.(i.e. not including prototype chain)

console.log(Object.keys(student)); // [‘name’, 'grade']//check whether is plain object:Object.keys(student).length === 0; //falseObject.keys({}).length === 0; //true

Object.getOwnPropertyNames returns an array of all self properties (including non-enumerable properties) found directly upon a given object.

// will not iterate through prototype chainconsole.log(Object.getOwnPropertyNames(student)); // [‘name’, ‘age’, 'grade', 'sex']

Summarize

methodsthrough prototype chainenumerable only
for...inYY
Object.keysNY
Object.getOwnPropertyNamesNN



作者:不吃猫的鱼_zjh
链接:https://www.jianshu.com/p/cc716f9797fa

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消