Traverse the object: for…in loops through the object’s own and inherited enumerable properties
Loops through the object’s own and inherited enumerable properties (excluding Symbol properties).
1
2
3
4var obj = {'0':'a', '1':'b', '2':'c'};
for(var i in obj){
console.log(i, ":", obj[i]);
}result:
1
2
30:a
1:b
2:c
Traverse objects: Object.keys()
Returns an array, including all enumerable properties (excluding Symbol properties) of the object itself (excluding inherited ones).
1
2
3
4var obj = {'0':'a', '1':'b', '2':'c'};
Object.keys(obj).forEach(function(key){
console.log(key, obj[key]);
});result:
1
2
30a
1b
2c
Traverse objects: Object.keys(obj), Object.values(obj)
1
2
3const obj = {id:1, name:"Derek", age:18};
console.log(Object.keys(obj));
console.log(Object.values(obj));result:
1
2['id', 'name', 'age']
['1', 'Derek', '18']
Traverse objects: Object.getOwnPropertyNames(obj)
Returns an array containing all properties of the object itself (excluding Symbol properties, but including non-enumerable properties).
1
2
3
4
5var obj = {'0':'a','1':'b','2':'c'};
Object.getOwnPropertyNames(obj).forEach(function(key){
console.log(key,obj[key]);
});
Traverse objects: Reflect.ownKeys(obj)
1
2
3
4
5var obj = {'0':'a','1':'b','2':'c'};
Reflect.ownKeys(obj).forEach(function(key){
console.log(key,obj[key]);
});result:
1
2
30a
1b
2c
Traverse objects: Object.entries()
Object.entries()
is a method in JavaScript that returns an array of key-value pairs for a given object’s own enumerable properties, the order of the key-value pairs in the array and the order of usingfor...in
loops Same (the main difference between the two is that thefor-in
loop also enumerates the properties in the prototype chain).Let’s say you have the following objects:
1
2
3
4
5const obj = {
a: "hello",
b: "world",
c: "!"
};If you call
Object.entries(obj)
you will get:1
2
3
4
5[
['a', 'hello'],
['b', 'world'],
['c', '!']
]In the code you gave,
Object.entries(fontSize)
converts each property of thefontSize
object into an array of the form[key, value]
, and then uses themap
method to map each key value Perform some operations on. In this operation, it creates a newHeadingBasestyle
component and usesheadingName
(key) as thekey
property of the component andheadingSize
(value) as theheadingsize
property.headingName
is also used Used as a child element of a component.Overall,
Object.entries()
is a way to get a list of key-value pairs from an object, which is useful when you need to perform operations on each property of the object.
Traverse the array: forEach()
1
2
3
4var arr=[1, 2, 3, 4];
arr.forEach(function(val, index){
console.log(val, index)
});result:
1
2
3
41 0
twenty one
3 2
4 3
Traverse the array: for..in..
1
2
3
4var arr=['Derek', 'Tom', 'Jason', 'Tim'];
for(var i in arr){
console.log(i, ":", arr[i]);
}result:
1
2
3
40:Derek
1:Tom
2: Jason
3: Tim
Traverse array: for-of
Not only arrays are supported, but also most array-like objects, such as DOM NodeList objects.
String traversal is also supported, which traverses strings as a series of Unicode characters.
1
2
3
4var arr=['Derek', 'Tom', 'Jason', 'Tim'];
for(var value of arr){
console.log(value);
}result:
1
2
3
4Derek
Tom
Jason
Tim