当前位置:网站首页>Methods for JS object to obtain attributes (. And [] methods)

Methods for JS object to obtain attributes (. And [] methods)

2022-07-06 07:21:00 Elephants and ants

js There are two ways to get properties of objects :1. adopt . The way 2. adopt [] The way

//  adopt . Method to get the property value ,key Is static 
var aa = {
    name: "zhang", age: 18};
console.log(aa.name);
 
//  adopt [] Get attribute value , key Is dynamic , Can be a string , Or in the form of numbers 
var bb = {
    "apple": 3, "pear": 2}
var cc = {
    1: "number1", 2: "number2"}
console.log(bb["apple"]);
console.log(cc[1]);  //  Note that the writing here is easily confused with array ,cc Still object , Is not an array 
 
//  Get all objects key Methods 
console.log(Object.keys(bb));  //  Output [ 'apple', 'pear' ]
原网站

版权声明
本文为[Elephants and ants]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060718558303.html