Language/JavaScript

[JS] ๐Ÿ“š map ๊ณ ์ฐจํ•จ์ˆ˜ - ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ฒŒ ์„ค๋ช…

์ธํŒŒ_ 2021. 9. 28. 20:06

๋ฐฐ์—ด๊ณ ์ฐจํ•จ์ˆ˜map

map ๊ณ ์ฐจํ•จ์ˆ˜

map์€ callback ํ•จ์ˆ˜๋ฅผ ๊ฐ๊ฐ์˜ ์š”์†Œ์— ๋Œ€ํ•ด ํ•œ๋ฒˆ์”ฉ ์ˆœ์„œ๋Œ€๋กœ ๋ถˆ๋Ÿฌ ๊ทธ ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜๊ฐ’์œผ๋กœ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋งŒ๋“ ๋‹ค. array๋ฅผ ๋Œ๋ฉด์„œ array ๋กœ ๊ฒฐ๊ณผ๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.

 

array.map(callbackFunction(currenValue, index, array), thisArg)

  • currentValue : ๋ฐฐ์—ด ๋‚ด ํ˜„์žฌ ๊ฐ’
  • index : ๋ฐฐ์—ด ๋‚ด ํ˜„์žฌ ๊ฐ’์˜ ์ธ๋ฑ์Šค
  • array : ํ˜„์žฌ ๋ฐฐ์—ด
  • thisArg : callbackFunction ๋‚ด์—์„œ this๋กœ ์‚ฌ์šฉ๋  ๊ฐ’
var numbers = [ 1,2,3,4,5,6,7,8,9];

function multiplyTwo(number){
    return number *2;
}
var newNumbers = numbers.map(multiplyTwo);
console.log(newNumbers); // [2, 4, 6, 8, 10, 12, 14, 16, 18]
var numbers = [4,9,16,25,36];
var result = numbers.map(Math.sqrt);
console.log(result); // [ 2, 3, 4, 5, 6 ]

map ํ™œ์šฉ ์‹ค์ „ ์˜ˆ์ œ

 

[๊ฐ์ฒด์—์„œ name๊ฐ’๋งŒ ์ถ”์ถœํ•ด์„œ ๋ฐฐ์—ด๋กœ ์ €์žฅ]

var students = [
  {id:1, name:"james"},
  {id:2, name:"tim"},
  {id:3, name:"john"},
  {id:4, name:"brian"}
];


// for๋ฌธ
var arr = new Array();
for (let obj of students){
 arr.push(obj.name);   
}
console.log(arr); // ['james', 'tim', 'john', 'brian']


// map
var names = students.map(student =>student.name);
console.log(names);  // ['james', 'tim', 'john', 'brian']

โ€‹

[array1๋ฐฐ์—ด์˜ ์š”์†Œ๋“ค์„ 2๋ฐฐ ํ•ด์ค€ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜]

const array1 = [1, 4, 9, 16];


// for๋ฌธ ํ™œ์šฉ
const mmm = (arr) => {
  const arr2 = [];
  for(let i of arr){
    arr2.push(i*2);
  }
  return arr2;
}


// map ํ•จ์ˆ˜ ํ™œ์šฉ
const map1 = array1.map(x => x * 2);


console.log(mmm(array1)); // [2,8,18,32]
console.log(map1); // [2,8,18,32]

 

[2์ฐจ์›๋ฐฐ์—ด ์ ‘๊ทผ]

var numbers = [[1,2,3],[4,5,6],[7,8,9]]; //array์•ˆ์— array๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ
var newNumbers = numbers.map(array => array.map(number => number *2));
console.log(newNumbers);

โ€‹

[๋ฆฌํ„ฐ๋Ÿด ํ˜•ํƒœ๋กœ ๋ฐฐ์—ด์„ ์ผ์ž๋กœ ์ „๋‹ฌ] (๊ตฌ์กฐ๋ถ„ํ•ด)

let prices = {
  banana: 1,
  orange: 2,
  meat: 4,
};

let doublePrices = Object.fromEntries(
  // ๊ฐ์ฒด๋ฅผ ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•ด์„œ ๋ฐฐ์—ด ์ „์šฉ ๋ฉ”์„œ๋“œ์ธ map์„ ์ ์šฉํ•˜๊ณ  fromEntries๋ฅผ ์‚ฌ์šฉํ•ด ๋ฐฐ์—ด์„ ๋‹ค์‹œ ๊ฐ์ฒด๋กœ ๋˜๋Œ๋ฆฝ๋‹ˆ๋‹ค.
  // [["banana", 1], ["orange", 2], ["meat", 4]]
  Object.entries(prices).map(([key, value]) => [key, value * 2])
);

alert(doublePrices.meat); // 8

โ€‹

[๊ฐ์ฒด๋ฅผ ๋ฐ›์•„์™€์„œ ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ด]

: DB์—์„œ ์ฟผ๋ฆฌ๊ฐ’๋“ค์„ ๋ฐฐ์—ด๊ฐ์ฒด๋กœ ์ €์žฅํ•˜๊ณ , ๊ธฐ์กด ํ•„๋“œ ๋Œ€์‹  ์‚ฌ์šฉ์ž ์ •์˜ ํ•„๋“œ๋ฅผ ์ƒ์„ฑํ•ด ๋‹ค์‹œ ๋ฐฐ์—ด๊ฐ์ฒด์— ์ €์žฅ

const userList = [
    {userId:"1", firstName:"Seungwon", lastName:"Go", yyyymmdd:"19770513", phone:"010-1111-1111", address:"Jeju"},
    {userId:"2", firstName:"Jeremy", lastName:"Go", yyyymmdd:"19800103", phone:"010-1111-1112", address:"Seoul"},
    {userId:"3", firstName:"James", lastName:"Go", yyyymmdd:"19821113", phone:"010-1111-1113", address:"Seoul"},
]; // DB์—์„œ ๊ฐ€์ ธ์˜จ ์ฟผ๋ฆฌ ์ •๋ณด๋ฅผ ๊ฐ์ฒด ํ˜•ํƒœ๋กœ ์ €์žฅ

var mon = ["Jan", "Feb", "Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];

const newUserList = userList.map(u => ({ // ๊ทธ๋ƒฅ u => { } ํ•˜๋ฉด ํ•จ์ˆ˜๊ฐ€ ๋˜์–ด๋ฒ„๋ฆฌ๋‹ˆ ๊ด„ํ˜ธ๋กœ ๊ฐ์‹ธ์„œ ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค๊ณ  ๋ช…์‹œ
    userId:u.userId, //๊ธฐ์กด ๋ฐ์ดํ„ฐ๋ฅผ ์ด์šฉํ•œ ์ƒˆ๋กœ์šด ํ•„๋“œ ์ถ”๊ฐ€
    // firstName:u.firstName, 
    // lastName:u.lastName, 
    fullName:u.firstName+' '+u.lastName,
    //yyyymmdd:u.yyyymmdd, 
    year: u.yyyymmdd.substring(0,4),
    month: u.yyyymmdd.substring(4,6),
    day: u.yyyymmdd.substring(6,8),
    usDataFormat: mon[parseInt(u.yyyymmdd.substring(4,6)) - 1] + " " + parseInt(u.yyyymmdd.substring(6,8)) + ", " + parseInt(u.yyyymmdd.substring(0,4)), 
    //๋‚˜๋ผ๋งˆ๋‹ค ๋…„์›”์ผ ์˜ค๋Š” ์ˆœ์„œ๊ฐ€ ๋‹ค๋ฅด๋‹ค. ์ด ํ‚ค๋Š” ์›”๋…„์ผ ๋กœ ์˜ค๊ฒŒ ํ•œ๋‹ค.
    phone:u.phone, 
    address:u.address
}));

console.log(newUserList);

์ •๋ฆฌํ•˜์ž๋ฉด, map์€ ๋ฐฐ์—ด์„ 1๋Œ€1๋กœ ์ง์ง“๋˜ ๊ธฐ์กด ๊ฐ์ฒด๋ฅผ ์ˆ˜์ •ํ•˜์ง€ ์•Š๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค.


๋ถ€๋ก) map ๊ณผ forEach ์ฐจ์ด์ 

โ€‹๊ณตํ†ต์  : array๊ด€๋ จ ๋ฉ”์„œ๋“œ

  • โ€‹forEach()๋Š” ๋ฐฐ์—ด ์š”์†Œ๋งˆ๋‹ค ํ•œ ๋ฒˆ์”ฉ ์ฃผ์–ด์ง„ ํ•จ์ˆ˜(์ฝœ๋ฐฑ)๋ฅผ ์‹คํ–‰
  • map()์€ ๋ฐฐ์—ด ๋‚ด์˜ ๋ชจ๋“  ์š”์†Œ ๊ฐ๊ฐ์— ๋Œ€ํ•˜์—ฌ ์ฃผ์–ด์ง„ ํ•จ์ˆ˜(์ฝœ๋ฐฑ)๋ฅผ ํ˜ธ์ถœํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ชจ์•„ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜

โ€‹

.forEach()๋Š” ๊ฒฐ๊ณผ๋กœ array๋ฅผ ๊ฐ€์ง€๊ธฐ ์œ„ํ•ด์„œ push๋ฅผ ํ†ตํ•ด ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์ค˜์•ผํ•จ

๋ฐฐ์—ด๊ณ ์ฐจํ•จ์ˆ˜map

โ€‹

.map()์€ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜

๋ฐฐ์—ด๊ณ ์ฐจํ•จ์ˆ˜map