The method of obtaining attributes of js objects (. and [] methods)

There are two ways to get properties of js object: 1. By way of . 2. By way of []

// Obtain the attribute value through ., the key is static
var aa = {name: "zhang", age: 18};
console.log(aa.name);
 
// Obtain the attribute value through [], the key is dynamic , can be a string or a number
var bb = {"apple": 3, "pear": 2}
var cc = {1: "number1", 2: "number2"}
console.log(bb["apple "]);
console.log(cc[1]); // Note that the wording here is easily confused with the array, cc is still an object, not an array. This way of writing is wrong, I am at https://www.w3schools.com The editor of /js/tryit.asp?filename=tryjs_map_create_array also tried it!
 
// method to get all keys of an object
console.log(Object.keys(bb)); // output [ 'apple', 'pear' ]

=========================================================================================== ====

The map object is [], such as: var map = new Map([
  ["apples", 500],
  ["bananas", 300],
  ["oranges", 200]
]);

The object is {}, such as: var aa = {name:"zhang",age:18};
But such an object can also be defined, such as: var bb = {"name":"goals",age:18}; / / This is regarded as a key-value pair?

Summary:
var aa = {name:"zhang",age:18};//key value does not have ""
var bb = {"name":"goals",age:18};//key value, one has " ", one without
var cc = {"name":"goals","age":18};//key value has ""

Value method:
aa.name can be
aa["name"] can be
aa[name] can't be
bb, and the result of cc is consistent with aa (at https://www.w3schools.com/js/tryit.asp?filename= tryjs_map_create_array editor)

Guess you like

Origin blog.csdn.net/Goals1989/article/details/125893112