当前位置:网站首页>Object value taking method in JS And []
Object value taking method in JS And []
2022-06-12 12:32:00 【I'm Mr.C】
JS Use . And Use [] The difference between values
JS There are two ways to get values for objects in —— Namely Use . And use []
Generally speaking , Object values are obtained by using .
How to write it :
let obj={
name:" nickname ",
value:" value ",
}
console.log(obj.name);// Output nickname
console.log(obj.value);// Output value
Use . It is common to take values in the form of , But you can't use string variables or numbers for attribute names
Like this :
// Wrong writing
obj.'name';
obj.10;
// This way of writing is wrong
// The best way to meet this requirement is to use [] To get the object value
Use [] Writing :
let obj={
name:"namename",
value:"content",
10:"ten",
}
console.log(obj[10]);// Output ten
console.log(obj['name']);// Output namename
It is [] The writing characteristics of , We can use for Loop to get a single object value
let arr=['name','value'];
let obj={
name:"111",
value:"222"
}
for(let i=0;i<=arr.length-1;i++){
console.log(obj[arr[i]]);
}
// Output 111
// 222
边栏推荐
- Is yuancosmos a short-term speculation or a future trend?
- 拿来就能用的网页动画特效,不来看看?
- TRON-api-波场转账查询接口-PHP版本-基于ThinkPHP5封装-附带接口文档-20220528版本
- 【Leetcode】637. Layer average of binary tree
- Quantization and Training of Neural Networks for Efficient Integer-Arithmetic-Only Inference
- JS string array converted to numeric array and how to add the numbers in the array
- Clone with cloneNode to solve the id problem / methods deep copy and shallow copy to modify the ID
- 22年gdcpc广东省赛记录
- Time series database - incluxdb2 docker installation
- Congratulations to splashtop for winning the 2022 it Europa "vertical application solution of the year" award
猜你喜欢

In navigation, the solution of adding borders to affect the layout

DOM+JS+轮播图+无时间

Promise understanding has used promise to realize picture preloading (sequential loading)

Reasons for college students' leave

Examples of Cartesian product and natural connection of relational algebra

Matlab install license manager error -8

JS attribute operation and node operation

Downloading and using SWI Prolog

Time series database - incluxdb2 docker installation

itk 多分辨率图像 itk::RecursiveMultiResolutionPyramidImageFilter
随机推荐
Numpy numerical calculation basis
[转]placement new
爱可可AI前沿推介(6.12)
Difference between Definition and Declaration
【您编码,我修复】WhiteSource正式更名为Mend
[JS] some handwriting functions: deep copy, bind, debounce, etc
Dom and BOM in JS
imx6-uboot添加lvds1显示
Vim,Gcc,Gdb
Is yuancosmos a short-term speculation or a future trend?
A short guide to SSH port forwarding
C语言深度解剖篇——关键字&&补充内容
You can't just use console Log ()?
sublime_ Textuse
DOM+JS+轮播图+无时间
Downloading and using SWI Prolog
sublime_text使用
屏蔽不显示VS警告warning
【Leetcode】416. Split equal sum subset
Preliminary study on Regional Simulation of crop model