File tree 1 file changed +24
-0
lines changed
1 file changed +24
-0
lines changed Original file line number Diff line number Diff line change @@ -43,4 +43,28 @@ console.log(getValue(mhs));
43
43
// Default Value - Multiple Fields
44
44
console . log ( getItems ( mhs ) ) ; // output: 123, Gagak Rimang
45
45
46
+ //------------------------------------------------------------------
46
47
48
+ // Extract fields by using .map method in Object Destructuring
49
+
50
+ // Object
51
+ const heroes = [
52
+ { name : 'Batman' , role : 'Good guy' } ,
53
+ { name : 'Joker' , role : 'Bad guy' }
54
+ ] ;
55
+
56
+ // Destructuring
57
+ const player = heroes . map (
58
+ function ( { name, role } ) {
59
+ return name + ' as ' + role ;
60
+ }
61
+ ) ;
62
+
63
+ const names = heroes . map (
64
+ function ( { name } ) {
65
+ return name ;
66
+ }
67
+ ) ;
68
+ // Result
69
+ console . log ( player ) ; // output: ["Batman as Good guy", "Joker as Bad guy"]
70
+ console . log ( names ) ; // output: ["Batman", "Joker"]
You can’t perform that action at this time.
0 commit comments