当前位置:网站首页>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']
边栏推荐
- CASE2023
- 29. 删除链表中重复的节点
- 【LeetCode每日一题】——103.二叉树的锯齿形层序遍历
- 淘宝详情.
- ofstream,ifstream,fstream read and write files
- 2022-07-30 mysql8 executes slow SQL-Q17 analysis
- PHP live source code to achieve simple barrage effect related code
- yaml
- BioVendor Human Club Cellular Protein (CC16) Elisa Kit Research Fields
- BI - SQL 丨 WHILE
猜你喜欢
Nanoprobes纳米探针丨Nanogold偶联物的特点和应用
The state status is displayed incorrectly after the openGauss switch
[Server data recovery] Data recovery case of server Raid5 array mdisk disk offline
【Unity入门计划】2D Game Kit:初步了解2D游戏组成
Outsourcing worked for three years, it was abolished...
oracle query scan full table and walk index
AWR分析报告问题求助:SQL如何可以从哪几个方面优化?
esp32经典蓝牙和单片机连接,,,手机蓝牙作为主机
2022-07-30 mysql8执行慢SQL-Q17分析
Project Background Technology Express
随机推荐
欧拉公式的证明
周鸿祎称微软抄袭,窃取360安全模式
Nanoprobes Polyhistidine (His-) Tag: Recombinant Protein Detection Protocol
使用self和_(下划线)的区别
NIO‘s Sword(牛客多校赛)
淘宝详情.
BioVendor Human Club Cellular Protein (CC16) Elisa Kit Research Fields
2022 NPDP take an examination of how the results?How to query?
swift project, sqlcipher3 -> 4, cannot open legacy database is there a way to fix it
【LeetCode每日一题】——704.二分查找
PHP live source code to achieve simple barrage effect related code
ApiFox 基本使用教程(浅尝辄止,非广)
From 2023 onwards, these regions will be able to obtain a certificate with a score lower than 45 in the soft examination.
Oracle19c安装图文教程
OC中成员变量,实例变量和属性之间的区别和联系
Service discovery of kubernetes
Safety (2)
记一次gorm事务及调试解决mysql死锁
Pinduoduo leverages the consumer expo to promote the upgrading of domestic agricultural products brands and keep pace with international high-quality agricultural products
oracle query scan full table and walk index