当前位置:网站首页>Object.entries()
Object.entries()
2022-08-02 12:21:00 【m0_67394006】
Object.entries() 方法返回一个给定对象自身可枚举属性的键值对数组。
其排列与使用 for...in 循环遍历该对象时返回的顺序一致(区别在于 for-in 循环还会枚举原型链中的属性)。
语法
Object.entries(obj)
参数
obj:可以返回其可枚举属性的键值对的对象。
返回值
给定对象自身可枚举属性的键值对数组。
描述
Object.entries()返回一个数组,其元素是与直接在object上找到的可枚举属性键值对相对应的数组。属性的顺序与通过手动循环对象的属性值所给出的顺序相同。
参数为对象
const obj = { name: 'xiaoming', age: 'seven',sex: 'man', grade: 'four' };
const res = Object.entries(obj)
console.log(res);
运行结果:

参数为数组
const obj = [1,2,3,4,5,6]
const res = Object.entries(obj)
console.log(res);
运行结果:

参数为数组(数组中包含对象 )
const obj = [1,2,3,4,5,6,{a:'a'},{b:'b'},{c:'c'}]
const res = Object.entries(obj)
console.log(res);
运行结果:
参数为数组(数组中元素为对象)
const obj = [{a:'a'},{b:'b'},{c:'c'}]
const res = Object.entries(obj)
console.log(res);
运行结果:
Object转换为Map
new Map()构造函数接受一个可迭代的entries。借助Object.entries方法你可以很容易的将Object转换为Map。
const obj = { name: 'xiaoming', age: 'seven',sex: 'man', grade: 'four' };
console.log(Object.entries(obj));
const map = new Map(Object.entries(obj));
console.log(map);
运行结果:

总结
Object.entries() 可以把一个对象的键值以数组的形式遍历出来,结果和 for...in 循环遍历该对象时返回的结果一样,但是不会遍历其原型属性。
先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
- 借小程序容器打造自有App小程序生态
- 【Acunetix-Forgot your password】
- SQL函数 TRIM
- ssm access database data error
- Distributed current limiting, hand & redisson implementation
- Learning Experience Sharing Seven: YOLOv5 Code Chinese Comments
- 第十四章 手动创建 REST 服务(二)
- Thymeleaf
- NVIDIA NeMo Metrics 轻量性能采集系统
- Likou 35 - search for insertion position - binary search
猜你喜欢
随机推荐
Distributed current limiting, hand & redisson implementation
SQL Server修改数据
中原银行实时风控体系建设实践
Leek 151 - Reverse words in a string
Data Lake (2): What is Hudi
太厉害了,终于有人能把TCP/IP 协议讲的明明白白了
MySQL主从复制几个重要的启动选项
元宇宙“吹鼓手”Unity:疯狂扩局,悬念犹存
According to the field classification Golang map array
Hand rolled architecture, 41 Redis interview asked
The ex-boyfriend bought chili water and threatened to rob his daughter. Can the woman apply for a personal safety protection order?
Create an application operation process using the kubesphere GUI
网站自动翻译-网站批量自动翻译-网站免费翻译导出
基于深度学习的裂缝检测技术
Pytorch 占用cpu资源过多
Intelligent Image Analysis-Intelligent Home Appliance Image Target Detection Statistical Counting Detection and Recognition-iCREDIT
[kali-information collection] (1.9) Metasploit + search engine tool Shodan
Crack detection technology based on deep learning
Transfer files between servers
从幻核疑似裁撤看如何保证NFT的安全









