当前位置:网站首页>JS中获取对象数据类型的键值对的键与值
JS中获取对象数据类型的键值对的键与值
2022-08-02 02:27:00 【博客zhu虎康】
Object.entries()
Object.entries()方法获取对象键值对
const object2 = {
a: 'somestring',
b: 42
};
for (const [key, value] of Object.entries(object2)) {
console.log(`${
key}: ${
value}`);
}
// output:
// "a: somestring"
// "b: 42"
Object.keys()
Object.keys 返回一个所有元素为字符串的数组,其元素来自于从给定的object上面可直接枚举的属性。这些属性的顺序与手动遍历该对象属性时的一致。
Object.keys( ) 会返回一个数组,数组中是这个对象的key值列表,所以只要Object.keys(a)[0], 就可以得只包含一个键值对的key值
// simple array
var arr = ['a', 'b', 'c'];
console.log(Object.keys(arr)); // console: ['0', '1', '2']
// array like object
var obj = {
0: 'a', 1: 'b', 2: 'c' };
console.log(Object.keys(obj)); // console: ['0', '1', '2']
Object.values()
Object.values()方法获取对象遍历的值
var obj1 = {
foo: 'bar', baz: 42 };
console.log(Object.values(obj1)); // ['bar', 42]
Object.keys()
Object.keys() 方法获取对象遍历的键
// simple array
var arr = ['a', 'b', 'c'];
console.log(Object.keys(arr)); // console: ['0', '1', '2']
// array like object
var obj = {
0: 'a', 1: 'b', 2: 'c' };
console.log(Object.keys(obj)); // console: ['0', '1', '2']
// array like object with random key ordering
var anObj = {
100: 'a', 2: 'b', 7: 'c' };
console.log(Object.keys(anObj)); // console: ['2', '7', '100']
边栏推荐
- 极大似然估计
- 2022-07-30 mysql8执行慢SQL-Q17分析
- 拼多多借力消博会推动国内农产品品牌升级 看齐国际精品农货
- 790. 数的三次方根
- TKU remembers a single-point QPS optimization (I wish ITEYE is finally back)
- How to adjust the cross cursor too small, CAD dream drawing calculation skills
- bool框架::PosInGrid (const简历:关键点kp, int &posX, int诗句)
- oracle query scan full table and walk index
- 周鸿祎称微软抄袭,窃取360安全模式
- 字符串常用方法
猜你喜欢

AI target segmentation capability for fast video cutout without green screen
![[Server data recovery] Data recovery case of server Raid5 array mdisk disk offline](/img/08/d693c7e2fff8343b55ff3c1f9317c6.jpg)
[Server data recovery] Data recovery case of server Raid5 array mdisk disk offline
![[LeetCode Daily Question] - 103. Zigzag Level Order Traversal of Binary Tree](/img/b9/35813ae2972375fa728e3c11fab5d3.png)
[LeetCode Daily Question] - 103. Zigzag Level Order Traversal of Binary Tree

Win Go development kit installation configuration, GoLand configuration

罗德里格斯公式(Rodrigues‘ Rotation Formula)推导

ApiFox 基本使用教程(浅尝辄止,非广)

MySQL8 download, start, configure, verify

Good News | AR opens a new model for the textile industry, and ALVA Systems wins another award!

The underlying data structure of Redis

BI-SQL丨WHILE
随机推荐
EFCore 反向工程
[LeetCode Daily Question] - 103. Zigzag Level Order Traversal of Binary Tree
Oracle19c安装图文教程
2022-08-01 Reflection
60 Feature Engineering Operations: Using Custom Aggregate Functions【Favorites】
C#测试项目中属性的用法
欧拉公式的证明
Speed up your programs with bitwise operations
项目场景 with ERRTYPE = cudaError CUDA failure 999 unknown error
FOFAHUB usage test
A good book for newcomers to the workplace
Safety (1)
四元数、罗德里格斯公式、欧拉角、旋转矩阵推导和资料
29. 删除链表中重复的节点
The state status is displayed incorrectly after the openGauss switch
CodeTon Round 2 D. Magical Array
Check if IP or port is blocked
789. 数的范围
Golang分布式应用之定时任务
PAT甲级打卡-1001-1004