当前位置:网站首页>js对象获取属性的方法(.和[]方式)
js对象获取属性的方法(.和[]方式)
2022-07-06 07:19:00 【大象与小蚂蚁】
js对象获取属性有两种方法:1.通过.的方式 2. 通过[]方式
// 通过.方式获取属性值,key是静态的
var aa = {
name: "zhang", age: 18};
console.log(aa.name);
// 通过[]获取属性值, key是动态的,可以是字符串,或者数字的形式
var bb = {
"apple": 3, "pear": 2}
var cc = {
1: "number1", 2: "number2"}
console.log(bb["apple"]);
console.log(cc[1]); // 注意这里的写法跟数组容易混淆,cc仍是对象,不是数组
// 获取对象所有key的方法
console.log(Object.keys(bb)); // 输出[ 'apple', 'pear' ]
边栏推荐
猜你喜欢
leetcode704. Binary search (find an element, simple, different writing)
Raspberry pie serial port login and SSH login methods
微信脑力比拼答题小程序_支持流量主带最新题库文件
Leetcode59. spiral matrix II (medium)
Oracle database 11gr2 uses TDE transparent data encryption to report an error ora28353. If you run to close the wallet, you will report an error ora28365. If you run to open the wallet, you will repor
Seriously recommend several machine learning official account
Short video, more and more boring?
leetcode6109. 知道秘密的人数(中等,周赛)
变量的命名规则十二条
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
随机推荐
杰理之AD 系列 MIDI 功能说明【篇】
Upgraded wechat tool applet source code for mobile phone detection - supports a variety of main traffic modes
Setting and using richview trvstyle template style
How MySQL merges data
word删除括号里内容
“无聊猿” BAYC 的内忧与外患
leetcode704. Binary search (find an element, simple, different writing)
leetcode1020. 飞地的数量(中等)
杰理之如若需要大包发送,需要手机端修改 MTU【篇】
杰理之BLE【篇】
leetcode704. 二分查找(查找某个元素,简单,不同写法)
The differences and advantages and disadvantages between cookies, seeion and token
Select all the lines with a symbol in word and change them to titles
Interface automation test framework: pytest+allure+excel
Applied stochastic process 01: basic concepts of stochastic process
Hydra common commands
Babbitt | metauniverse daily must read: the group image of Chinese Internet enterprises pouring into metauniverse: "there are only various survival desires, and there is no ambition for forward-lookin
巴比特 | 元宇宙每日必读:中国互联网企业涌入元宇宙的群像:“只有各种求生欲,没有前瞻创新的雄心”...
ORACLE列转行--某字段按指定分隔符转多行
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower