本文发布于 2022-11-22, 最后更新于 1361 天前,其中的信息可能已经有所发展或是发生改变。

文章摘要

属性的遍历 ES6一共有7种方法可以遍历对象的属性 1. for…in for...in循环遍历对象自身的和继承的可枚举属性(不含Symbol属性) const obj = { firstProp: '1', secondProp: 2, lastProp: true, }; const len = Symbol('length'); obj len] = 3; // for-in 遍历,只能遍历…

属性的遍历

ES6一共有7种方法可以遍历对象的属性

1. for…in

dart (Auto identified)
const obj = { firstProp: '1', secondProp: 2, lastProp: true, }; const len = Symbol('length'); obj[len] = 3; // for-in 遍历,只能遍历对象 // eslint-disable-next-line no-restricted-syntax console.log('// for...in') for (const item in obj) { console.log(item); }
text
// for...in firstProp secondProp lastProp

2. Object.getOwnPropertyNames(obj)

dns (Auto identified)
const obj = { firstProp: '1', secondProp: 2, lastProp: true, }; const len = Symbol('length'); obj[len] = 3; console.log('// Object.getOwnPropertyNames(obj)'); console.log(Object.getOwnPropertyNames(obj)) console.log('// keyName, obj[keyName]'); Object.getOwnPropertyNames(obj).forEach(function(keyName){ console.log(keyName, obj[keyName]); });
text
// Object.getOwnPropertyNames(obj) ['firstProp', 'secondProp', 'lastProp'] // keyName, obj[keyName] firstProp 1 secondProp 2 lastProp true

3. Object.getOwnPropertySymbols(obj)

dns (Auto identified)
const obj = { firstProp: '1', secondProp: 2, lastProp: true, }; const len = Symbol('length'); obj[len] = 3; console.log('// Object.getOwnPropertySymbols(obj)'); console.log(Object.getOwnPropertySymbols(obj)) console.log('// symbolName, obj[symbolName]'); Object.getOwnPropertySymbols(obj).forEach(function(symbolName){ console.log(symbolName, obj[symbolName]); });
text
// Object.getOwnPropertySymbols(obj) [Symbol(length)] // symbolName,obj[symbolName] Symbol(length) 3

4. Reflect.ownKeys(obj)

dart (Auto identified)
const obj = { firstProp: '1', secondProp: 2, lastProp: true, }; const len = Symbol('length'); obj[len] = 3; console.log('// Reflect.ownKeys(obj)') console.log(Reflect.ownKeys(obj));
text
// Reflect.ownKeys(obj) ['firstProp', 'secondProp', 'lastProp', Symbol(length)]

5. Object.keys(obj)

dart (Auto identified)
const obj = { firstProp: '1', secondProp: 2, lastProp: true, }; const len = Symbol('length'); obj[len] = 3; const keys = Object.keys(obj); console.log('// Object.keys'); console.log(keys);
text
// Object.keys [ 'firstProp', 'secondProp', 'lastProp' ]

6. Object.values(obj)

dart (Auto identified)
const obj = { firstProp: '1', secondProp: 2, lastProp: true, }; const len = Symbol('length'); obj[len] = 3; const values= Object.values(obj); console.log('// Object.values'); console.log(values);
text
// Object.values ['1', 2, true]

7. Object.entries(obj)

dns (Auto identified)
const obj = { firstProp: '1', secondProp: 2, lastProp: true, }; const len = Symbol('length'); obj[len] = 3; const entries= Object.entries(obj); console.log('// Object.entries'); console.log(entries); console.log('// foreach output Object.entries'); entries.forEach(function(entry){ console.log(entry); }) console.log('// for...of output Object.entries'); for (const [key, value] of entries) { console.log(key, value); }
css (Auto identified)
// Object.entries [ [ 'firstProp', '1' ], [ 'secondProp', 2 ], [ 'lastProp', true ] ] // foreach output Object.entries [ 'firstProp', '1' ] [ 'secondProp', 2 ] [ 'lastProp', true ] // for...of output Object.entries firstProp 1 secondProp 2 lastProp true

汇总

代码

dns (Auto identified)
const obj = { firstProp: '1', secondProp: 2, lastProp: true, }; const len = Symbol('length'); obj[len] = 3; // for-in 遍历,只能遍历对象 // eslint-disable-next-line no-restricted-syntax console.log('// for...in') for (const item in obj) { console.log(item); } console.log('// Object.getOwnPropertyNames(obj)'); console.log(Object.getOwnPropertyNames(obj)) console.log('// keyName, obj[keyName]'); Object.getOwnPropertyNames(obj).forEach(function (keyName) { console.log(keyName, obj[keyName]); }); console.log('// Object.getOwnPropertySymbols(obj)'); console.log(Object.getOwnPropertySymbols(obj)) console.log('// symbolName, obj[symbolName]'); Object.getOwnPropertySymbols(obj).forEach(function (symbolName) { console.log(symbolName, obj[symbolName]); }); console.log('// Reflect.ownKeys(obj)') console.log(Reflect.ownKeys(obj)); const keys = Object.keys(obj); console.log('// Object.keys'); console.log(keys); const values = Object.values(obj); console.log('// Object.values'); console.log(values); const entries = Object.entries(obj); console.log('// Object.entries'); console.log(entries); console.log('// foreach output Object.entries'); entries.forEach(function (entry) { console.log(entry); }) console.log('// for...of output Object.entries'); for (const [key, value] of entries) { console.log(key, value); }

预期输出

stylus (Auto identified)
// for...in firstProp secondProp lastProp // Object.getOwnPropertyNames(obj) [ 'firstProp', 'secondProp', 'lastProp' ] // keyName, obj[keyName] firstProp 1 secondProp 2 lastProp true // Object.getOwnPropertySymbols(obj) [ Symbol(length) ] // symbolName, obj[symbolName] Symbol(length) 3 // Reflect.ownKeys(obj) [ 'firstProp', 'secondProp', 'lastProp', Symbol(length) ] // Object.keys [ 'firstProp', 'secondProp', 'lastProp' ] // Object.values [ '1', 2, true ] // Object.entries [ [ 'firstProp', '1' ], [ 'secondProp', 2 ], [ 'lastProp', true ] ] // foreach output Object.entries [ 'firstProp', '1' ] [ 'secondProp', 2 ] [ 'lastProp', true ] // for...of output Object.entries firstProp 1 secondProp 2 lastProp true

九仞之行

严于律己,宽以待人,深自警省,讷言敏行

订阅
💬 评论 (0)

还没有评论呢 ~ 快来抢沙发吧 🛋️

编辑器加载中…