当前位置:网站首页>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']
边栏推荐
- C#测试项目中属性的用法
- BioVendor Human Club Cellular Protein (CC16) Elisa Kit Research Fields
- 周鸿祎称微软抄袭,窃取360安全模式
- Software testing Interface automation testing Pytest framework encapsulates requests library Encapsulates unified request and multiple base path processing Interface association encapsulation Test cas
- 1688API
- ofstream,ifstream,fstream read and write files
- 字符串常用方法
- 【web】理解 Cookie 和 Session 机制
- Nanoprobes丨1-巯基-(三甘醇)甲醚功能化金纳米颗粒
- 力扣(LeetCode)213. 打家劫舍 II(2022.08.01)
猜你喜欢

【LeetCode Daily Question】——704. Binary Search

The failure to create a role in Dahua Westward Journey has been solved

ros多客户端请求服务

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

How engineers treat open source

canal同步Mariadb到Mysql

BioVendor Human Club Cellular Protein (CC16) Elisa Kit Research Fields

FOFAHUB usage test

Win Go development kit installation configuration, GoLand configuration

openGauss切换后state状态显示不对
随机推荐
2022年NPDP考完多久出成绩?怎么查询?
微信小程序异步回调函数恶梦和解决办法
Rasa 3 x learning series - Rasa - 4873 dispatcher Issues. Utter_message study notes
2022-07-30 mysql8执行慢SQL-Q17分析
mysql 查看死锁
60 Feature Engineering Operations: Using Custom Aggregate Functions【Favorites】
2022-07-30 mysql8 executes slow SQL-Q17 analysis
线程的不同状态
CodeTon Round 2 D. Magical Array
永磁同步电机36问(三)——SVPWM代码实现
接口测试神器Apifox究竟有多香?
swift project, sqlcipher3 -> 4, cannot open legacy database is there a way to fix it
Rasa 3.x 学习系列- Rasa - Issues 4873 dispatcher.utter_message 学习笔记
IMU预积分的简单理解
[ORB_SLAM2] void Frame::ComputeImageBounds(const cv::Mat & imLeft)
Handwriting a blogging platform ~ Day 3
2022-08-01 mysql/stoonedb慢SQL-Q18分析
PHP live source code to achieve simple barrage effect related code
四元数、罗德里格斯公式、欧拉角、旋转矩阵推导和资料
NIO's Sword