当前位置:网站首页>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']
边栏推荐
- 【Unity入门计划】2D Game Kit:初步了解2D游戏组成
- 局部敏感哈希:如何在常数时间内搜索Embedding最近邻
- Good News | AR opens a new model for the textile industry, and ALVA Systems wins another award!
- 接口测试神器Apifox究竟有多香?
- yaml
- From 2023 onwards, these regions will be able to obtain a certificate with a score lower than 45 in the soft examination.
- 线程的不同状态
- openGauss切换后state状态显示不对
- 罗德里格斯公式(Rodrigues‘ Rotation Formula)推导
- 优炫数据库导库导错了能恢复吗?
猜你喜欢

IMU预积分的简单理解

Reflex WMS Intermediate Series 7: What should I do if I want to cancel the picking of an HD that has finished picking but has not yet been loaded?

Entry name 'org/apache/commons/codec/language/bm/gen_approx_greeklatin.txt' collided

2022-08-01 mysql/stoonedb slow SQL-Q18 analysis

qt点云配准软件

MySQL8 download, start, configure, verify

2022牛客多校四_G M

GTK RGB图像绘制

ofstream,ifstream,fstream read and write files

Talking about the "horizontal, vertical and vertical" development trend of domestic ERP
随机推荐
灰度传感器、、、diy原理。。图
TKU remembers a single-point QPS optimization (I wish ITEYE is finally back)
字符串常用方法
The first time I wrote a programming interview question for Niu Ke: input a string and return the letter with the most occurrences of the string
【LeetCode每日一题】——704.二分查找
Nanoprobes纳米探针丨Nanogold偶联物的特点和应用
Reflex WMS Intermediate Series 7: What should I do if I want to cancel the picking of an HD that has finished picking but has not yet been loaded?
[LeetCode Daily Question]——654. The largest binary tree
罗德里格斯公式(Rodrigues‘ Rotation Formula)推导
1688API
Golang分布式应用之定时任务
2022 Henan Youth Training League Game (3)
Swift运行时(派发机制)
力扣(LeetCode)213. 打家劫舍 II(2022.08.01)
字典常用方法
Nanoprobes多组氨酸 (His-) 标签标记:重组蛋白检测方案
ALCCIKERS Shane 20191114
canal同步Mariadb到Mysql
Moonbeam and Project integration of the Galaxy, bring brand-new user experience for the community
C language inserted into the characters of simple exercises