当前位置:网站首页>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']
边栏推荐
- How engineers treat open source
- Check if IP or port is blocked
- Win Go development kit installation configuration, GoLand configuration
- 接口测试神器Apifox究竟有多香?
- 【LeetCode每日一题】——103.二叉树的锯齿形层序遍历
- mysql 查看死锁
- 个人博客系统项目测试
- 数值积分方法:欧拉积分、中点积分和龙格-库塔法积分
- Use DBeaver for mysql data backup and recovery
- Electronic Manufacturing Warehouse Barcode Management System Solution
猜你喜欢

Personal blog system project test

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

The state status is displayed incorrectly after the openGauss switch

工程师如何对待开源
![[Unity entry plan] 2D Game Kit: A preliminary understanding of the composition of 2D games](/img/8a/07ca69c6dcc22757156cb615e241f8.png)
[Unity entry plan] 2D Game Kit: A preliminary understanding of the composition of 2D games

Win Go development kit installation configuration, GoLand configuration

qt点云配准软件

FOFAHUB usage test

【web】理解 Cookie 和 Session 机制

Oracle数据类型介绍
随机推荐
NAS和私有云盘的区别?1篇文章说清楚
IMU预积分的简单理解
2022-08-01 mysql/stoonedb慢SQL-Q18分析
极大似然估计
优炫数据库导库导错了能恢复吗?
Simple example of libcurl accessing url saved as file
个人博客系统项目测试
51. 数字排列
Moonbeam and Project integration of the Galaxy, bring brand-new user experience for the community
ofstream,ifstream,fstream read and write files
Nanoprobes丨1-巯基-(三甘醇)甲醚功能化金纳米颗粒
Oracle数据类型介绍
swift project, sqlcipher3 -> 4, cannot open legacy database is there a way to fix it
1688API
LeetCode 213. Robbery II (2022.08.01)
【LeetCode每日一题】——103.二叉树的锯齿形层序遍历
Analysis of the status quo of digital transformation of manufacturing enterprises
2022-08-01 Reflection
¶ Backtop back to the top is not effective
Chopper webshell feature analysis