Comparison of JavaScript object iteration methods and performance

Object.entries Returns all enumerable key-value pairs of the object, and will not trace the key on the prototype chain let obj = { key1: 'value1', key2: 'value2', key3: 'value3', } Object.entries(obj).forEach(entry => { let key = entry[0] let value = entry[1] // entry will be like this [“key1”, “value1”] }) Object.keys Returns all enumerable keys of the object let obj = { key1: 'value1', key2: 'value2', key3: 'value3', } Object.keys(obj).forEach(key => { let value = obj[key] }) Object.values Returns all enumerable values ​​of the object let obj = { key1: 'value1', key2: 'value2', key3: 'value3', } Object.values(obj).forEach(value => { // Only value can be used }) for…in loop Iterative enumerable properties will be searched along the prototype chain let obj = { key1: 'value1', key2: 'value2', key3: 'value3', } for (const key in obj) { let value = obj[key] if (obj.hasOwnProperty(key)) { // self } else { // from the prototype chain } } Object.getOwnPropertyNames Returns all (including non-enumerable) keys of the object (the original text says that it will look for the prototype chain, which is wrong ) let obj = { key1: 'value1', key2: 'value2', key3: 'value3', } Object.getOwnPropertyNames(obj).forEach(key => { let value = obj[key] }) Performance comparison…

Comparison of JavaScript Object Iteration Methods and Performance

Object.entries Returns all enumerable key-value pairs of the object, and will not track the key on the prototype chain let obj = { key1: ‘value1’, key2: ‘value2’, key3: & # 39; value3 & # 39;, } Object.entries(obj).forEach(entry => { let key = entry[0] let value = entry[1] // entry will be like [“key1”, “value1”] }) Object.keys Return all enumerable keys of the object let obj = { key1: ‘value1’, key2: ‘value2’, key3: & # 39; value3 & # 39;, } Object.keys(obj).forEach(key => { let value = obj[key] }) Object.values Return all enumerable values ​​of the object let obj = { key1: ‘value1’, key2: ‘value2’, key3: & # 39; value3 & # 39;, } Object.values(obj).forEach(value => { // only use value }) for…in loop Iterative enumerable properties will be found along the prototype chain let obj = { key1: ‘value1’, key2: ‘value2’, key3: & # 39; value3 & # 39;, } for (const key in obj) { let value = obj[key] if (obj. hasOwnProperty(key)) { // self } else { // from the prototype chain } } Object.getOwnPropertyNames Return all (including non-enumerable) keys of the object (the original text said that it will find the prototype chain is wrong ) let…

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索