kohigowild
JavaScript Map() ๋ฉ์๋ ์ดํดํ๊ธฐ ๋ณธ๋ฌธ
๐ Map ์ ์
- Map() ๋ฉ์๋๋ ๋ฐฐ์ด ๋ด ๋ชจ๋ ์์์ ๋ํ์ฌ callbackFunction์ ์คํํ ๊ฒฐ๊ณผ๋ฅผ ๋ชจ์ ์๋ก์ด ๋ฐฐ์ด์ ๋ฐํํ ๋ ์ฌ์ฉํ๋ค.
- Map() ๋ฉ์๋๋ ์๋ก์ด ์๋ฃ๊ตฌ์กฐ ํน์ ๊ฐ์ฒด๋ฅผ ์ ์ํ ๋ ์ฌ์ฉํ๋ค. ๊ทธ ์ค์์๋ ์๋ก์ด ์๋ฃ๊ตฌ์กฐ๋ฅผ ํ์๋ก ํ๋ ๊ฒฝ์ฐ์ ๋ง์ด ์ฌ์ฉํ๋ค.
- ์๋ฅผ ๋ค์ด, ์ด๋ค ์๋น์ค์ ๊ฐ์ ํ ํ์๋ค์ ์ด๋ฆ, ๋์ด, ์ฐ๋ฝ์ฒ๋ก ๋๋์ด์ง ๊ฐ๊ฐ์ ๋ฐ์ดํฐ๊ฐ ์ฃผ์ด์ก์ ๋ ๋ชจ๋ ๋ฐ์ดํฐ๋ฅผ ํฉ์น ํ์ ์ ๋ณด๋ฅผ ์ฌ์ฉํด์ผ ํ๋ ์ํฉ์ map() ๋ฉ์๋๋ฅผ ํ์ฉํ ์ ์๋ค.
- callbackFunction, thisArg ๋ ๊ฐ์ ์ธ์๋ฅผ ๊ฐ๊ณ , callbackfunction์ currentValue, index, array ์ธ ๊ฐ์ ์ธ์๋ฅผ ๊ฐ๋๋ค.
arr.map(callback(currentValue[, index[, array]])[, thisArg])
callbackFunction
- ์ด๋ค ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ๊ฑฐ๋ ํน์ ์์ ์ ๋๋ฌํ์ ๋ ์์คํ ์์ ํธ์ถํ๋ ํจ์
- ๋ค๋ฅธ ์ฝ๋์ ์ธ์๋ก ๋๊ฒจ์ฃผ๋ ํจ์ (์ ์ด๊ถ๋ ํจ๊ป ์์)
currentValue
- ๋ฐฐ์ด์ ์์ ์ค ํ์ฌ ๊ฐ
index (Optional)
- ํ์ฌ ๊ฐ์ ์ธ๋ฑ์ค
array (Optional)
- map() ๋ฉ์๋์ ๋์์ด ๋๋ ๋ฐฐ์ด ์์ฒด
thisArg (Optional)
- callback Function ๋ด๋ถ์์ **this๋ก ์ธ์ํ ๋์
- ์๋ตํ ๊ฒฝ์ฐ ์ผ๋ฐ์ ์ธ ํจ์์ ๋ง์ฐฌ๊ฐ์ง๋ก ์ ์ญ ๊ฐ์ฒด๊ฐ ๋ฐ์ธ๋ฉ
** this: ์์ ์ด ์ํ ๊ฐ์ฒด ๋๋ ์์ ์ด ์์ฑํ ์ธ์คํด์ค๋ฅผ ๊ฐ๋ฆฌํค๋ ์๊ธฐ ์ฐธ์กฐ ๋ณ์
๐ For loop์ Map ๋น๊ต ์์
๋ฐฐ์ด nums์ 1์ ๋ํ๊ณ ์ ํ๋ค.
// for loop ์ฌ์ฉ
const nums = [0, 10, 100, 1000, 10000];
const result = [];
for (i = 0; i < nums.length; i++) {
result.push(nums[i] + 1);
if (i == 3) break;
}
console.log(result);
// [ 1, 11, 101, 1001 ]
// map ์ฌ์ฉ
const nums = [0, 10, 100, 1000, 10000];
const result = nums.map((nums) => nums + 1);
console.log(result);
// [ 1, 11, 101, 1001, 10001 ]
- MAP์ ๋ฐฐ์ด์ ์ํํ๋ฏ๋ก ์ค๊ฐ์ “break;” ๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ๋ถ๊ฐ๋ฅํ๋ค. ์ด๋ฐ ๊ฒฝ์ฐ๋ผ๋ฉด for loop๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค.
- MAP์ ํตํด ์๋ก์ด ๋ฐฐ์ด(result)์ ๋ฐํํ์๋ค. ํธ์ถํ ๋ฐฐ์ด(nums)์ ๊ฐ์ ๋ณํํ์ง ์๋๋ค.
๐ MAP() ์์ 01 | ๋ฐฐ์ด์ ๊ฐ ๋ฌธ์์ด ๊ฐ์ ์ธ๊ธฐ
const name = ["MITT", "JIMIN", "MIEUN", "SUNWOO"];
const lengthOfName = name.map((name) => name.length);
console.log(lengthOfName);
// [ 4, 5, 5, 6 ]
๐ฌ MAP์ ํตํด name์ ๊ฐ ์์๋ค์ length๋ฅผ ํธ์ถํ ๊ฒฐ๊ณผ๋ฅผ ๋ชจ์, lengthOfName์ด๋ผ๋ ์๋ก์ด ๋ฐฐ์ด์ ๋ฐํํ์๋ค.
๐ MAP() ์์ 02 | ๋ฐฐ์ด ์ ๊ฐ์ฒด ์ฌ๊ตฌ์ฑ
const arr = [
{ name: "MITT", age: 28 },
{ name: "SOPHIE", age: 26 },
{ name: "PICKLE", age: 23 },
];
const new_arr = arr.map(function (obj) {
const rObj = {};
rObj[obj.name] = obj.age;
return rObj;
});
console.log(arr);
/* [ { name: 'MITT', age: 28 },
{ name: 'SOPHIE', age: 26 },
{ name: 'PICKLE', age: 23 }] */
console.log(new_arr);
// [ { MITT: 28 }, { SOPHIE: 26 }, { PICKLE: 23 } ]
๐ map ์์ 03 | ์ฐ๋ด 10% ์ธ์
const employee = [
{ name: "MITT", rank: "manager", salary: 6000 },
{ name: "MIEUN", rank: "supervisor", salary: 4800 },
{ name: "JIMIN", rank: "staff", salary: 3100 },
{ name: "SUNWOO", rank: "staff", salary: 3000 },
];
const raisedSalaryList = employee.map((employee) => employee.salary * 1.1);
raisedSalaryList.forEach((salary) => console.log("salary : %d", salary));
/* salary : 6600
salary : 5280
salary : 3410
salary : 3300 */
'JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
JavaScript ํ๋กํ ํ์ ๊ณผ ํ๋กํ ํ์ ์ฒด์ธ (0) | 2022.10.21 |
---|---|
JavaScript ํ(Queue), ์คํ(Stack), ํธ๋ฆฌ(Tree) (0) | 2022.10.20 |
JavaScript ์ ๊ท ํํ์(Regular Expression) (0) | 2022.10.16 |
JavaScript ๋ฐฐ์ด ๊ณ ์ฐจ ํจ์(Higher-Order Function) (0) | 2022.10.13 |
JavaScript ๊ฐ์ฒด(Object)์ ๊ธฐ์ด (0) | 2022.10.08 |