自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

JS 對(duì)象遍歷知多少?

開(kāi)發(fā) 前端
本文主要是對(duì) JS 對(duì)象(不含數(shù)組、Map、Set 結(jié)構(gòu))的 7個(gè)遍歷方法進(jìn)行總結(jié)歸納,本文會(huì)以艾弗森的基本資料為模板生成一個(gè) JS 對(duì)象并對(duì)其進(jìn)行遍歷 。

[[412480]]

最前面

本文主要是對(duì) JS 對(duì)象(不含數(shù)組、Map、Set 結(jié)構(gòu))的 7個(gè)遍歷方法進(jìn)行總結(jié)歸納,寫(xiě)該文章的這天恰好是我最喜愛(ài)的球星艾弗森的 45 周歲生日,因此本文會(huì)以艾弗森的基本資料為模板生成一個(gè) JS 對(duì)象并對(duì)其進(jìn)行遍歷 。

定義對(duì)象

首先讓我們定義如以下代碼所示的對(duì)象 player:

  1. const player = { 
  2.     name'Allen  Iverson'
  3.     [Symbol('birthday')]: '1975/06/07'
  4. }; 
  5. Object.defineProperties(player, { 
  6.     isHallofFame: { 
  7.         enumerable: false
  8.         value: true
  9.     }, 
  10.     [Symbol('ScoreKingTime')]: { 
  11.         enumerable:false
  12.         value: 4, 
  13.     }, 
  14. }); 
  15. Object.defineProperties(player.__proto__, { 
  16.     university: { 
  17.         enumerable: true
  18.         value: 'Georgetown'
  19.     }, 
  20.     team: { 
  21.         enumerable: false
  22.         value: '76ers'
  23.     }, 
  24.     [Symbol('country')]: { 
  25.         enumerable: true
  26.         value: 'USA'
  27.     }, 
  28.     [Symbol('hometown')]: { 
  29.         enumerable: false
  30.         value: 'Virginia'
  31.     }, 
  32. }); 

如上述代碼所示,定義了一個(gè) player 的對(duì)象,其共有以下 8 個(gè)屬性:

通過(guò)控制臺(tái)的輸出觀察也更直觀:

其中淺顏色的屬性都是不可枚舉的屬性,__proto__下的屬性則為其原型上(即 Object.prototype)的屬性,Symbol 類(lèi)型的值自然為 Symbol 屬性

for...in

MDN:The for...in statement iterates over all enumerable properties of an object that are keyed by strings (ignoring ones keyed by Symbols), including inherited enumerable properties.

  1. for(const name in player) { 
  2.     console.log('name:'name); 
  3. // namename 
  4. // name: university 

for...in 循環(huán)是我們較為常用的遍歷對(duì)象方法了,結(jié)合 MDN 里所言以及輸出結(jié)果不難得出其遍歷的屬性,包含自身以及原型上所有可枚舉的屬性,不包含 Symbol 屬性。因?yàn)槠淇杀闅v到原型上可枚舉的屬性,因此我們的代碼中通常會(huì)多出一句這樣的判斷(如果我們不需要原型上的屬性):

  1. for(const name in player) { 
  2.     if (Object.prototype.hasOwnProperty.call(player, name)) { 
  3.         console.log('name:'name); 
  4.     } 
  5.      
  6. // namename 

Object.keys

MDN:The Object.keys() method returns an array of a given object's own enumerable property names, iterated in the same order that a normal loop would.

  1. const keys = Object.keys(player); 
  2. console.log('keys:', keys); 
  3. // keys: ["name"

Object.keys 大概是我們最常用的遍歷方法了,如在 Vue 源碼對(duì) data 進(jìn)行遍歷響應(yīng)式處理也是用這個(gè)方法。通過(guò)輸出結(jié)果也表明:其返回的是所有自身可枚舉的屬性(不含 Symbol 屬性),不包含原型上的任何屬性。

Object.getOwnPropertyNames

MDN:The Object.getOwnPropertyNames() method returns an array of all properties (including non-enumerable properties except for those which use Symbol) found directly in a given object.

  1. const ownPropertyNames = Object.getOwnPropertyNames(player); 
  2. console.log('ownPropertyNames:', ownPropertyNames); 
  3. // ownPropertyNames: ["name""isHallofFame"

Object.getOwnPropertyNames 返回的是所有自身的屬性(包含不可枚舉屬性但不包含 Symbol 屬性),不包含原型上的任何屬性。

Object.getOwnPropertySymbols

MDN:The Object.getOwnPropertySymbols() method returns an array of all symbol properties found directly upon a given object.

  1. onst ownPropertySymbols  = Object.getOwnPropertySymbols(player); 
  2. console.log('ownPropertySymbols:', ownPropertySymbols); 
  3. // ownPropertySymbols: [Symbol(birthday), Symbol(ScoreKingTime)] 

通過(guò)輸出不難得出 Object.getOwnPropertySymbols 返回的是自身的所有 Symbol 屬性(包含不可枚舉的),但是不含原型上的任何屬性。

Reflect.ownKeys

MDN:The static Reflect.ownKeys() method returns an array of the target object's own property keys.

  1. const ownKeys = Reflect.ownKeys(player); 
  2. console.log('ownKeys:', ownKeys) 
  3. // ownKeys: ["name""isHallofFame", Symbol(birthday), Symbol(ScoreKingTime)] 

Reflect.ownKeys 返回的是自身的所有屬性(包含不可枚舉的以及所有 Symbol 屬性),不包含原型 上的任何屬性。

Object.values

MDN:The Object.values() method returns an array of a given object's own enumerable property values, in the same order as that provided by a for...in loop. (The only difference is that a for...in loop enumerates properties in the prototype chain as well.)

  1. const values = Object.values(player); 
  2. console.log('values:'values); 
  3. // values: ["Allen  Iverson"

Object.values 同 Object.keys 一樣,其遍歷的是所有自身可枚舉的屬性(不含 Symbol 屬性),不包含原型上的任何屬性。但與 Object.keys 不同的是:其返回的不再是 key 值而是 value 值集合。

Object.entries

MDN: The Object.entries() method returns an array of a given object's own enumerable string-keyed property [key, value] pairs, in the same order as that provided by a for...in loop. (The only important difference is that a for...in loop enumerates properties in the prototype chain as well).

  1. const entries =Object.entries(player); 
  2. console.log('entries:', entries); 
  3. // entries: [["name""Allen  Iverson"]] 

其也同 Object.keys 一樣,遍歷的是所有自身可枚舉的屬性(不含 Symbol 屬性),不包含原型上的任何屬性。不同的是,其返回值是 [key, value]的集合。Object.entries 在我們平時(shí)的開(kāi)發(fā)中可能很少用到,但是其可配合Object.fromEntries一起使用:有以下代碼:

  1. // 對(duì)象轉(zhuǎn)換 
  2. const object1 = { a: 1, b: 2, c: 3 }; 
  3. const object2 = Object.fromEntries( 
  4.   Object.entries(object1) 
  5.   .map(([ key, val ]) => [ key, val * 2 ]) 
  6. ); 
  7. console.log(object2); 
  8. // { a: 2, b: 4, c: 6 } 

總結(jié)

結(jié)合文章中的代碼輸出結(jié)果可得到以下表格:

簡(jiǎn)而言之:

  • 只有 for...in 可以遍歷到原型上的屬性
  • Object.getOwnPropertyNames 和 Reflect.ownKeys 可獲取到不可枚舉的屬性
  • Object.getOwnPropertySymbols 和 Reflect.ownKeys 可獲取到 Symbol 屬性

 

責(zé)任編輯:姜華 來(lái)源: 微醫(yī)大前端技術(shù)
相關(guān)推薦

2012-02-13 22:50:59

集群高可用

2024-08-06 10:07:15

2010-08-16 09:15:57

2013-12-23 14:00:31

Windows 8.2Windows 8.1

2021-12-04 11:17:32

Javascript繼承編程

2025-04-14 08:50:00

Google ADK人工智能AI

2017-07-14 10:51:37

性能優(yōu)化SQL性能分析

2009-05-13 17:31:06

DBAOracleIT

2012-09-10 16:38:40

Windows Ser

2020-09-08 10:56:55

Java多線程存儲(chǔ)器

2018-08-31 10:53:25

MySQL存儲(chǔ)引擎

2018-12-12 15:01:22

開(kāi)源存儲(chǔ) 軟件

2013-08-02 09:42:37

BYODBYOC云存儲(chǔ)

2009-03-06 19:19:55

2021-12-09 06:41:56

Python協(xié)程多并發(fā)

2022-01-06 16:20:04

Java排序算法排序

2022-05-08 18:02:11

tunnel隧道云原生

2010-09-29 09:28:04

DHCP工作原理

2024-07-01 12:30:09

2010-06-17 19:07:12

UML對(duì)象
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)