<HTML>
<body>
</body>
<script>
let john = { name: "John", surname: "Smith", id: 1 };
let pete = { name: "Pete", surname: "Hunt", id: 2 };
let mary = { name: "Mary", surname: "Key", id: 3 };
let users = [ john, pete, mary ];
let usersMapped = users.map(function(item){
let obj = {
fullName : item.name + " " + item.surname,
id : item.id
};
return obj;
});
alert( usersMapped[0].id ) // 1
alert( usersMapped[0].fullName ) // John Smith
</script>
</HTML>
<!--
solution coding
let usersMapped = users.map(user => ({
fullName: `${user.name} ${user.surname}`,
id : user.id
}));
1. uses shorten version of function writing
2. () is to say what it does every term of mapping,
is calling this anonymous function which make and return
the objeect mapped
3. {} means this function will return a object!!!
-->
<body>
</body>
<script>
let john = { name: "John", surname: "Smith", id: 1 };
let pete = { name: "Pete", surname: "Hunt", id: 2 };
let mary = { name: "Mary", surname: "Key", id: 3 };
let users = [ john, pete, mary ];
let usersMapped = users.map(function(item){
let obj = {
fullName : item.name + " " + item.surname,
id : item.id
};
return obj;
});
alert( usersMapped[0].id ) // 1
alert( usersMapped[0].fullName ) // John Smith
</script>
</HTML>
<!--
solution coding
let usersMapped = users.map(user => ({
fullName: `${user.name} ${user.surname}`,
id : user.id
}));
1. uses shorten version of function writing
2. () is to say what it does every term of mapping,
is calling this anonymous function which make and return
the objeect mapped
3. {} means this function will return a object!!!
-->
댓글
댓글 쓰기