Skip to content

Commit 9927479

Browse files
authored
Update Extract-Fields-of-Function-in-Object-Destructuring.js
1 parent 1094dac commit 9927479

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Extract-Fields-of-Function-in-Object-Destructuring.js

+24
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,28 @@ console.log(getValue(mhs));
4343
// Default Value - Multiple Fields
4444
console.log(getItems(mhs)); // output: 123, Gagak Rimang
4545

46+
//------------------------------------------------------------------
4647

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"]

0 commit comments

Comments
 (0)