Once in a while, you may need to loop through objects in JavaScript. The only way to do so before ES6 was with a for...in loop.

有时,您可能需要遍历JavaScript中的对象。 在ES6之前这样做的唯一方法是使用for...in循环。

The problem with a for...in loop is that it iterates through properties in the Prototype chain. When you loop through an object with the for...in loop, you need to check if the property belongs to the object. You can do this with hasOwnProperty.

for...in循环的问题在于它会循环访问原型链中的属性。 当使用for...in循环遍历对象时,需要检查该属性是否属于该对象。 您可以使用hasOwnProperty做到这一点。

for (var property in object) {  if (object.hasOwnProperty(property)) {    // Do things here  }}

We no longer have to rely on for...in and hasOwnProperty now. There’s a better way.

现在,我们不再需要for...inhasOwnProperty 。 有更好的方法。

遍历对象的更好方法 (A better way to loop through objects)

The better way to loop through objects is first to convert the object into an array. Then, you loop through the array.

遍历对象的更好方法是首先将对象转换为数组。 然后,您遍历数组。

You can convert an object into an array with three methods:

您可以使用以下三种方法将对象转换为数组:

  1. Object.keys

    Object.keys

  2. Object.values

    Object.values

  3. Object.entries

    Object.entries

对象键 (Object.keys)

Object.keys creates an array that contains the properties of an object. Here’s an example.

Object.keys创建一个包含对象属性的数组。 这是一个例子。

const fruits = { apple: 28, orange: 17, pear: 54 };
const keys = Object.keys(fruits);console.log(keys); // ["apple", "orange", "pear"]

对象值 (Object.values)

Object.values creates an array that contains the values of every property in an object. Here’s an example:

Object.values创建一个数组,其中包含对象中每个属性的值。 这是一个例子:

const fruits = { apple: 28, orange: 17, pear: 54 };
const values = Object.values(fruits);console.log(values); // [28, 17, 54]

对象条目 (Object.entries)

Object.entries creates an array of arrays. Each inner array has two items. The first item is the property; the second item is the value.

Object.entries创建一个数组数组。 每个内部数组都有两个项目。 第一项是财产; 第二项是值。

Here’s an example:

这是一个例子:

const fruits = { apple: 28, orange: 17, pear: 54 };
const entries = Object.entries(fruits);console.log(entries); // [["apple", 28], ["orange", 17], ["pear", 54]]

My favorite of the three is Object.entries, because you get both the key and property values.

我最喜欢的三个是Object.entries ,因为您同时获得了键和属性值。

遍历数组 (Looping through the array)

Once you’ve converted the object into an array with Object.keys, Object.values, or Object.entries, you can loop through it as if it was a normal array.

一旦将对象转换为具有Object.keysObject.valuesObject.entries的数组,就可以像遍历普通数组一样遍历该对象。

const fruits = { apple: 28, orange: 17, pear: 54 };
// Looping through arrays created from Object.keys
const keys = Object.keys(fruits);for (const key of keys) {  console.log(keys);
}// ["apple", "orange", "pear"]
// ["apple", "orange", "pear"]
// ["apple", "orange", "pear"]

If you use Object.entries, you might want to destructure the array into its key and property.

如果使用Object.entries ,则可能需要将数组解构为其键和属性。

const fruits = { apple: 28, orange: 17, pear: 54 };
const entries = Object.entries(fruits);for (const [fruit, count] of entries) {console.log(`There are ${count} ${fruit}s`);
}// "There are 28 apples"
// "There are 17 oranges"
// "There are 54 pears"

结语 (Wrapping up)

The better way to loop through objects is first convert it into an array with one of these three methods.

遍历对象的更好方法是首先使用这三种方法之一将其转换为数组。

  1. Object.keys

    Object.keys

  2. Object.values

    Object.values

  3. Object.entries

    Object.entries

Then, you loop through the results like a normal array.

然后,像普通数组一样循环遍历结果。

If this lesson has helped you, might enjoy Learn JavaScript, where you’ll learn how to build anything you want from scratch. Enrollment for Learn JavaScript opens in July 2018 (very soon!).

如果本课程对您有所帮助,请享受Learn JavaScript ,在这里您将学习如何从头开始构建任何内容。 学习JavaScript的注册将于2018年7月开放(很快!)。

Thanks for reading. Did this article help you in any way? If I did, I hope you consider sharing it; you might just help someone who felt the same way you did before reading the article. Thank you.

谢谢阅读。 本文对您有任何帮助吗? 如果我做到了, 我希望你考虑分享它 ; 您可能只是帮助与您在阅读本文之前一样的人。 谢谢。

This article was originally posted at my blog. Sign up for my newsletter if you want more articles to help you become a better frontend developer.

本文最初发布在我的博客上 。 如果您想获得更多文章来帮助您成为更好的前端开发人员,请注册我的时事通讯 。

翻译自: https://www.freecodecamp.org/news/how-to-loop-through-objects-in-javascript-a80b7d2478ac/

如何遍历JavaScript中的对象相关推荐

  1. 遍历JavaScript中的数组

    在Java中,可以使用for循环遍历数组中的对象,如下所示: String[] myStringArray = {"Hello", "World"}; for ...

  2. JavaScript中Object对象深度遍历

    Object对象深度遍历 支持Object和Array混合型对象遍历.其中用到的对象类型判断方法,详见<JavaScript中Object对象类型判断> // 递归实现 // obj 为父 ...

  3. name optimize is not defined 怎么解决_Web前端:怎么在JavaScript中比较对象?

    大家好,我来了,本期为大家带来的前端开发知识是"Web前端:怎么在JavaScript中比较对象?",有兴趣做前端的朋友,一起来看看吧! 比较JavaScript中的原始值很简单. ...

  4. JavaScript 中的对象拷贝(深拷贝、浅拷贝)

    对象是 JavaScript 的基本块.对象是属性的集合,属性是键值对.JavaScript 中的几乎所有对象都是位于原型链顶部 Object 的实例. 介绍 如你所知,赋值运算符不会创建一个对象的副 ...

  5. 如何检查数组是否包含JavaScript中的对象?

    In this article, we will look at various methods to check if an array includes an object in JavaScri ...

  6. 如何从JavaScript中的对象数组中获得不同的值?

    本文翻译自:How to get distinct values from an array of objects in JavaScript? Assuming I have the followi ...

  7. 对象删除某个属性_充分了解JavaScript中【对象】的概念(二)

    点击上方「蓝字」关注我们 之前的文章: 充分了解JavaScript中[对象]的概念(一) 这篇文章我们继续来讲解JavaScript中[对象]的概念,因为这是一系列的文章,所以建议从第一篇文章开始看 ...

  8. Javascript中的对象拷贝(对象复制/克隆)

    Javascript中的对象拷贝(对象复制/克隆) 李俊才 CSDN:jcLee95 邮箱:291148484@163.com 1. 对象的引用 要说"拷贝"还要先说"引 ...

  9. JavaScript中的对象

    什么是对象 现实生活中:万物皆对象,对象是一个具体的事物,一个具体的事物就会有行为和特征. 举例: 一部车,一个手机 车是一类事物,门口停的那辆车才是对象     特征:红色.四个轮子     行为: ...

最新文章

  1. Go 学习笔记(32)— 类型系统(命名类型、未命名类型、底层类型、类型强制转换、类型别名和新声明类型)
  2. 【计算机网络】数据链路相关技术
  3. C++返回char*第n个位置开始的子字符串
  4. leetcode - Recover Binary Search Tree
  5. python 占位符 %z_python-如何用z替换熊猫数据框中的负数
  6. 《深入解析windows操作系统第6版下册》第10章:内存管理(第三部分译文与图片)...
  7. SNS源码大集合--转载ITPUB
  8. gridview 排序
  9. 机器视觉培训教程-硬件选型
  10. vue代码查重(任意格式文件)
  11. python罗盘时钟代码_jQuery css3创意的罗盘时钟代码
  12. 服务器cadence比虚拟机慢,Cadence版本选择浅见
  13. Arai2自动添加tracker链接的方法
  14. python列表同时添加多个元素_python怎么向列表中添加多个元素
  15. 德鲁克:卓有成效的管理者,一次只做一件事
  16. Vue 图片压缩并上传至服务器
  17. 2021年全国城镇单位就业人员平均工资数据出炉
  18. Gartner2021新兴技术和趋势影响力雷达图:四项颇具影响力的技术
  19. 三款Android平台音乐播放器多方位对比
  20. Maven虐我千百遍,我待Maven如初恋!

热门文章

  1. book3 复习 使用c#开发控制台应用程序
  2. ListView控件获取选中项的内容 c# 114867417
  3. 案例 银行取款 java 1615136927
  4. 爬虫-访问登陆可见的页面-利用session类-补实例
  5. TIDB2.1版本升级步骤
  6. C/C++基本数据类型
  7. mac的safari浏览器调试h5
  8. Python连接mysql基本操作
  9. Android-构建不同环境的Apk
  10. 数据结构之线性表-链式存储之单链表(一)