Javascript ์ •๋ฆฌ(7)

kirin.logยท2021๋…„ 2์›” 5์ผ
0

โœ… Number

๐Ÿ‘‰ Math.round
๋ฐ˜์˜ฌ๋ฆผ ํ•จ์ˆ˜ (์†Œ์ˆซ์ ์ด 5 ์ด์ƒ์ด๋ฉด ์˜ฌ๋ฆผ)

console.log(Math.round(2.5));   // 3
console.log(Math.round(2.49));  // 2
console.log(Math.round(2));     // 2
console.log(Math.round(2.82));  // 3 

๐Ÿšฉ '๋ฉœ๋ก ' ์•ฑ์—์„œ ์‚ฌ๋žŒ๋“ค์ด ๋…ธ๋ž˜ ํ‰์ ์„ ๋งค๊ธธ ๋•Œ, ๋ณ„ 0~5๊ฐœ ์ค‘์— ์„ ํƒํ•ด์•ผ ํ•œ๋‹ค๋ฉด, ๋ชจ๋“  ์‚ฌ๋žŒ๋“ค์˜ ์ ์ˆ˜๋ฅผ ๋”ํ•ด ํ‰๊ท ์„ ๋‚ด๋ฉด ์†Œ์ˆ˜์ ์ด ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋‹ค(ํ‰๊ท  3.56)
์ด๋Ÿด ๋•Œ Math.round ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ˜์˜ฌ๋ฆผ ํ•œ ๊ฐ’์„ ๋‚ผ ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ‘‰ Math.floor
๋‚ด๋ฆผ ํ•จ์ˆ˜ (์†Œ์ˆซ์ ์€ ๋‹ค ์—†์•ฐ)

console.log(Math.floor(2.5));   // 2
console.log(Math.floor(2.49));  // 2
console.log(Math.floor(2));     // 2
console.log(Math.floor(2.82));  // 2

๐Ÿ‘‰ Math.ceil
์˜ฌ๋ฆผ ํ•จ์ˆ˜ (์†Œ์ˆซ์ ์€ ๋‹ค ์˜ฌ๋ฆผ)

console.log(Math.ceil(2.5));   // 3
console.log(Math.ceil(2.49));  // 3
console.log(Math.ceil(2));     // 2
console.log(Math.ceil(2.82));  // 3

๐Ÿš€ Math๊ฐ์ฒด ๊ด€๋ จ ์ฐธ๊ณ  site
https://www.w3schools.com/js/js_math.asp


๐Ÿ‘‰ ๋žœ๋คํ•จ์ˆ˜

โ›ต Math.random();

let randomNumber = Math.random();
console.log(randomNumber);

โฉ 0.00000000000์—์„œ 0.99999999999 ์‚ฌ์ด์˜ ๊ฐ’์—์„œ ๋žœ๋ค์ˆ˜๋ฅผ ์ œ๊ณต


โ›ต Math.floor();

var randomNumber = Math.random();
console.log(Math.floor(randomNumber*10));

โฉ randomNumber*10 ์˜ ๊ฐ’์€ 1์˜ ์ž๋ฆฌ์ˆ˜์— ์—„์ฒญ ๊ธด ์†Œ์ˆ˜์ž๋ฆฌ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค.
ex) 9.697009826327621
โ— Math.random๊ณผ Math.floor๋ฅผ ํ™œ์šฉํ•˜์—ฌ 0~10์‚ฌ์ด์˜ ๋žœ๋ค์ˆ˜๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค
โœจโœจ ๋กœ๋˜, ์ด๋ฒคํŠธ ๋‹น์ฒจ์ž ๋“ฑ์„ ๋ฝ‘์„ ๋•Œ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉ


โ›ต min <= randomNumber(์ •์ˆ˜) <= max

// ์ตœ๋Œ“๊ฐ’, ์ตœ์†Ÿ๊ฐ’ ํฌํ•จ
function randomNumber(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min + 1)) + min; //์ตœ๋Œ“๊ฐ’๋„ ํฌํ•จ, ์ตœ์†Ÿ๊ฐ’๋„ ํฌํ•จ
}

โ›ต min <= randomNumber(์ •์ˆ˜) < max

// ์ตœ์†Ÿ๊ฐ’ ํฌํ•จ, ์ตœ๋Œ“๊ฐ’ ๋ฏธํฌํ•จ
function getRandomInt(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min)) + min; //์ตœ๋Œ“๊ฐ’์€ ์ œ์™ธ, ์ตœ์†Ÿ๊ฐ’์€ ํฌํ•จ
}
/* 
์ตœ์†Œ(min), ์ตœ๋Œ€๊ฐ’(max)์„ ๋ฐ›์•„ ๊ทธ ์‚ฌ์ด์˜ ๋žœ๋ค์ˆ˜๋ฅผ returnํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•ด์ฃผ์„ธ์š”.
*/
function randomNumber (min, max) {
  let randomNum = Math.random();
  return Math.floor(randomNum*(max-min+1))+min; //์ตœ๋Œ“๊ฐ’๋„ ํฌํ•จ, ์ตœ์†Ÿ๊ฐ’๋„ ํฌํ•จ
}
randomNumber (5,9);  

โœ… Object

  • ๊ฐ์ฒด๋Š” { }(์ค‘๊ด„ํ˜ธ)๋กœ ๊ฐ์‹ธ์ ธ ์žˆ๋‹ค
  • property ์ด๋ฆ„์€ ์ค‘๋ณต๋  ์ˆ˜ ์—†๋‹ค.
  • property ์ด๋ฆ„๊ณผ property ๊ฐ’ ์‚ฌ์ด์— :(์ฝœ๋ก )์œผ๋กœ ๊ตฌ๋ถ„ํ•œ๋‹ค.
  • ์‰ผํ‘œ๋กœ ๋ถ„๋ฆฌ๋œ ๋ชฉ๋ก์˜ ํ˜•ํƒœ(property๋ฅผ ์ถ”๊ฐ€ํ•  ๋•Œ๋Š” ,(์‰ผํ‘œ)๋ฅผ ๋ถ™์—ฌ์ค€๋‹ค.)
  • property ๊ฐ’์—๋Š” ์–ด๋Š type์ด๋‚˜ ๊ฐ€๋Šฅํ•˜๋‹ค(string, number, array, object, function..)
    โฉ ๋‹ค์‹œ ๋งํ•˜๋ฉด ๊ฐ์ฒด๋Š” ์ด๋ฆ„๊ณผ ๊ฐ’์œผ๋กœ ๊ตฌ์„ฑ๋œ ํ”„๋กœํผํ‹ฐ๋“ค์˜ ์ง‘ํ•ฉ์ด๋‹ค.

๐Ÿ‘‰ ๊ฐ์ฒด์˜ property๊ฐ’์— ์ ‘๊ทผ

๋ฐฉ๋ฒ•(1) ๋งˆ์นจํ‘œ(.) ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉ
์ ‘๊ทผํ•˜๋ ค๋Š” ๊ฐ์ฒด๋ช…์€ ์™ผ์ชฝ์—, ํ”„๋กœํผํ‹ฐ๋ช…์€ ์˜ค๋ฅธ์ชฝ์— ์œ„์น˜.

๋ฐฉ๋ฒ•(2) ๋Œ€๊ด„ํ˜ธ([])๋ฅผ ์‚ฌ์šฉ
์ ‘๊ทผํ•˜๋ ค๋Š” ๊ฐ์ฒด๋ช…์€ ์™ผ์ชฝ์—, ํ”„๋กœํผํ‹ฐ๋ช…์€ ์Œ๋”ฐ์˜ดํ‘œ("")์™€ ํ•จ๊ป˜ ๋Œ€๊ด„ํ˜ธ ์•ˆ์— ์ž‘์„ฑ.
โ— ๋Œ€๊ด„ํ˜ธ ์•ˆ์—๋Š” ๋ณ€์ˆ˜๊ฐ€ ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋‹ค.

let plan1 = {   
  name: "Basic"
};

// ๋ฐฉ๋ฒ•(1) ๋งˆ์นจํ‘œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์†์„ฑ๊ฐ’์— ์ ‘๊ทผ
console.log(plan1.name);

// ๋ฐฉ๋ฒ•(2) ๋Œ€๊ด„ํ˜ธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์†์„ฑ๊ฐ’์— ์ ‘๊ทผ
console.log(plan1["name"]);



let plan1 = {   
  name: "Basic"
};

let propertyName = "name";

// ๋ฐฉ๋ฒ•(2) ๋Œ€๊ด„ํ˜ธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์†์„ฑ๊ฐ’์— ์ ‘๊ทผ(๋ณ€์ˆ˜๊ฐ€ ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๊ธฐ๋•Œ๋ฌธ)
console.log(plan1[propertyName]);





let myObj = {
  property1: "hello",      // ์†์„ฑ๊ฐ’์œผ๋กœ ์ˆซ์ž(Number)
  property2: [1,2,3,4,5],  // ์†์„ฑ๊ฐ’์œผ๋กœ ๋ฐฐ์—ด(object) 
  property3: {             // ์†์„ฑ๊ฐ’์œผ๋กœ ํ•จ์ˆ˜(object)
    childproperty: "haha"
  }
};

let name = "property";
console.log(myObj[name+"1"]);  // =console.log(myObj[property1] = "hello" ์ถœ๋ ฅ
console.log(myObj[name+"2"]);  // =console.log(myObj[property2] = "[1,2,3,4,5]" ์ถœ๋ ฅ
console.log(myObj[name+"3"]); // =console.log(myObj[property3]) = "{ childproperty: "haha" }" ์ถœ๋ ฅ
console.log(myObj[name+"3"]["child"+name]);  // =console.log(myObj[property3][childproperty]) = "haha" ์ถœ๋ ฅ
  
console.log(myObj.property1);  // 'hello'
console.log(myObj.property2);  // [1,2,3,4,5]
console.log(myObj.property3);  // { childproperty: 'jaja' }
console.log(myObj.property3.childproperty);   // 'haha'
/* 
- ์ธ์ž๋กœ ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํ•ฉํ•ด์„œ, ๊ตฌ์กฐ๋ฅผ ๋ฐ”๊ฟ”์„œ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.
- getData์— ๋ฐฐ์—ด ์„ธ ๊ฐœ๋ฅผ ์ธ์ž๋กœ ๋„˜๊ธธ ๊ฒƒ์ด๊ณ , ์ด ๋ฐฐ์—ด ์„ธ ๊ฐœ๋ฅผ ์กฐํ•ฉํ•ด์„œ ํ•˜๋‚˜์˜ ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค ๊ฒƒ์ž…๋‹ˆ๋‹ค.
- getData๋Š” ์„ธ ๊ฐœ์˜ ๋ฐฐ์—ด์„ ์ธ์ž๋กœ ๋ฐ›๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
    - salesArr: ๋‚ ์งœ๋ณ„ ํŒ๋งค๋Ÿ‰
    - ex) [["20190401", 34], ["20190402", 23], ["20190403", 29]]
    - reviewArr: ๋‚ ์งœ๋ณ„ ๋ฆฌ๋ทฐ์ˆ˜
    - ex) [["20190328", 3], ["20190401", 0], ["20190403", 1]]
    - likeArr: ๋‚ ์งœ๋ณ„ ์ข‹์•„์š”์ˆ˜
    - ex) [["20190328", 98], ["20190401", 102], ["20190403", 125]]
์œ„์˜ ์˜ˆ์‹œ๋กœ ๋ณด์—ฌ๋“œ๋ฆฐ ๋ฐฐ์—ด์€ ๋‹จ์ง€ ์˜ˆ์ผ๋ฟ, ๋‚ ์งœ๋ณ„ ํŒ๋งค๋Ÿ‰ ๋ฐฐ์—ด์€ 365์ผ์น˜ ์š”์†Œ๊ฐ€ ๋“ค์–ด์žˆ์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.
๊ฐ์ฒด๋Š” ํ”„๋กœํผํ‹ฐ๋ช…์œผ๋กœ ๋ฐ”๋กœ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๋‹ˆ๊นŒ, ๋‚ ์งœ๋งŒ ์•Œ๋ฉด ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ์ง€ ์•Š์•„๋„ ๋ฐ”๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๊ฐ์ฒด์˜ ํ˜•ํƒœ๋กœ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋ฅผ ๋ฐ”๊พธ๋ ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.
๋‹ค์Œ ์š”๊ตฌ์‚ฌํ•ญ์„ ์ถฉ์กฑํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด์„œ return ํ•ด์ฃผ์„ธ์š”.
objData๊ฐ€ ๊ฐ–๊ณ  ์žˆ๋Š” property๋Š” 3๊ฐœ ์ž…๋‹ˆ๋‹ค.
- sumAmount: ์ด ํŒ๋งค๋Ÿ‰
- sumReview: ์ด ๋ฆฌ๋ทฐ๊ฐœ์ˆ˜
- sumLike: ์ด ์ข‹์•„์š”์ˆ˜
*/
const salesArr = [["20190401", 34], ["20190402", 23], ["20190403", 29]]
const reviewArr = [["20190328", 3], ["20190401", 0], ["20190403", 1]]
const likeArr = [["20190328", 98], ["20190401", 102], ["20190403", 125]]


let salesSum = 0
for (let i=0; i<salesArr.length; i++) {
  salesSum += salesArr[i][1]
}

let reviewSum = 0
for (let i=0; i<reviewArr.length; i++) {
  reviewSum += reviewArr[i][1]
}

let likeSum = 0
for (let i=0; i<likeArr.length; i++) {
  likeSum += likeArr[i][1]
}


const objData = {
  sumAmount : salesSum,
  sumReview : reviewSum ,
  sumLike :  likeSum
}

โœ… includes ( ) ๋ฉ”์†Œ๋“œ

๐Ÿ‘‰ includes ( )
๋ฐฐ์—ด ๋˜๋Š” ๋ฌธ์ž์—ด์ด ํŠน์ • ์š”์†Œ๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๋ฉ”์†Œ๋“œ.

โ— ๋ฌธ์ž๋‚˜ ๋ฌธ์ž์—ด์„ ๋น„๊ตํ•  ๋•Œ, includes()๋Š” ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„

๐Ÿ‘‰ fromIndex
arr.includes(valueToFind, fromIndex)
โ—พ ๋ฌธ์ž์—ด์—์„œ ์ฐพ๊ธฐ ์‹œ์ž‘ํ•˜๋Š” ์œ„์น˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ธ๋ฑ์Šค ๊ฐ’. ๊ธฐ๋ณธ๊ฐ’์€ 0. ์–ด๋–ค ์ •์ˆ˜๋ผ๋„ ๊ฐ€๋Šฅ.
โ—พ ๋งŒ์•ฝ fromIndex๊ฐ’์ด ์Œ์˜ ์ •์ˆ˜์ด๋ฉด ์ „์ฒด ๋ฌธ์ž์—ด์„ ์ฐพ๊ฒŒ ๋œ๋‹ค.
โ—พ fromIndex๊ฐ€ ๋ฐฐ์—ด์˜ ๊ธธ์ด๋ณด๋‹ค ๊ฐ™๊ฑฐ๋‚˜ ํฌ๋ฉด(fromIndex >= str.length) false ๋ฐ˜ํ™˜.
โ—พ valueToFind ๊ฐ’์„ ์ฐพ์„ ์ˆ˜ ์—†์œผ๋ฉด -1 ๋ฐ˜ํ™˜

string.includes ( searchString, length )

// searchString: ๊ฒ€์ƒ‰ํ•  ๋ฌธ์ž์—ด๋กœ ํ•„์ˆ˜์š”์†Œ. ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„ํ•จ
// length: ๊ฒ€์ƒ‰์„ ์‹œ์ž‘ํ•  ์œ„์น˜. ์„ ํƒ์š”์†Œ๋กœ์จ ๊ฐ’์ด ์—†์œผ๋ฉด ์ „์ฒด ๋ฌธ์ž์—ด์„ ๋Œ€์ƒ์œผ๋กœ ํ•œ๋‹ค.
'abzcd'.includes('z')     // true ์ถœ๋ ฅ ('abzcd'๊ฐ€ 'z'๋ฅผ ํฌํ•จํ•˜๋Š”์ง€ ๊ฒ€์‚ฌ)
'abzcd'.includes('z', 2)  // true ์ถœ๋ ฅ ('zcd'๊ฐ€ 'z'๋ฅผ ํฌํ•จํ•˜๋Š”์ง€ ๊ฒ€์‚ฌ)
'abzcd'.includes('z', 3)  // false ์ถœ๋ ฅ ('cd'๊ฐ€ 'z'๋ฅผ ํฌํ•จํ•˜๋Š”์ง€ ๊ฒ€์‚ฌ)
'abzcd'.includes('Z', 3 ) // false ์ถœ๋ ฅ (๋Œ€์†Œ๋ถ„์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜๋ฏ€๋กœ ๋Œ€๋ฌธ์ž'Z'๋Š” ์—†๊ธฐ๋•Œ๋ฌธ์— false.

const arr = [1,2,3];
console.log(arr.includes(2));  // true ์ถœ๋ ฅ

const pets = ['cat', 'dog', 'bat'];
console.log(pets.includes('cat'));  // true ์ถœ๋ ฅ

console.log(pets.includes('at'));   // false ์ถœ๋ ฅ

[1, 2, 3].includes(2);     // true (2๊ฐ€ ์กด์žฌ)
[1, 2, 3].includes(4);     // false (4๊ฐ€ ์กด์žฌ)
[1, 2, 3].includes(3, 3);  // false (์ „์ฒด ๋ฌธ์ž์—ด์€ 2๋ฒˆ์งธ ๊นŒ์ง€์ด๋ฏ€๋กœ 3์€ ์ „์ฒด ๋ฌธ์ž์—ด๋ณด๋‹ค ํฌ๊ธฐ ๋•Œ๋ฌธ์— false)
[1, 2, 3].includes(3, -1); // true  (fromIndex๊ฐ’์ด ์Œ์˜ ์ •์ˆ˜์ด๋ฉด ์ „์ฒด ๋ฌธ์ž์—ด์—์„œ ์ฐพ๋Š”๋‹ค. 3์ด ์žˆ์œผ๋ฏ€๋กœ true)
[1, 2, NaN].includes(NaN); // true

โœ… switch ๋ฌธ

๐Ÿ‘‰ ์กฐ๊ฑด ์ œ์–ด๋ฌธ์— ์†ํ•œ๋‹ค (cf. if ๋ฌธ) ๊ทธ๋Ÿฌ๋‚˜ if ๋ฌธ์€ ์กฐ๊ฑด์‹์— ๋ถ€๋“ฑ์‹(<,<=,>,>=)์ด ์‚ฌ์šฉ๋˜์ง€๋งŒ switch๋ฌธ์€ ์˜ค์ง ==๋งŒ ๋น„๊ตํ•  ์ˆ˜ ์žˆ๋‹ค.
โ— if๋ฌธ์€ ์กฐ๊ฑด์‹์ด true์ผ ๊ฒฝ์šฐ์— block์ด ์‹คํ–‰๋œ๋‹ค๊ณ  ํ•˜๋ฉด, switch๋ฌธ์€ ๋น„๊ตํ•  ๋ณ€์ˆ˜๊ฐ€ ์–ด๋–ค ๊ฐ’์„ ๊ฐ€์ง€๋ƒ์— ๋”ฐ๋ผ ์‹คํ–‰๋ฌธ์„ ์„ ํƒํ•œ๋‹ค.)

switch (๋ณ€์ˆ˜) {
   case ๊ฐ’1 :
         ์‹คํ–‰๋ฌธ;
         break;
   case ๊ฐ’2 :
         ์‹คํ–‰๋ฌธ;
         break;
   default :
         ์‹คํ–‰๋ฌธ;
}

โ— switch๋ฌธ์€ ๊ด„ํ˜ธ ์•ˆ์˜ (๋ณ€์ˆ˜)์˜ ๊ฐ’๊ณผ ๋™์ผํ•œ ๊ฐ’์„ ๊ฐ–๋Š” case๋กœ ๊ฐ€์„œ ์‹คํ–‰๋ฌธ์„ ์‹คํ–‰์‹œํ‚จ๋‹ค. (case์‹คํ–‰ ํ›„ break์ด ๋˜์–ด switch๋ฌธ์ด ์ข…๋ฃŒ๋œ๋‹ค)
๋งŒ์•ฝ ๊ด„ํ˜ธ์•ˆ์˜ (๋ณ€์ˆ˜)์˜ ๊ฐ’๊ณผ ๋™์ผํ•œ ๊ฐ’์˜ case๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋‹ค๋ฉด default๋กœ ๊ฐ€์„œ ์‹คํ–‰๋ฌธ์„ ์‹คํ–‰์‹œํ‚จ๋‹ค(default๋Š” ์ƒ๋žต ๊ฐ€๋Šฅ)

let score = 80;

switch (score/10) {
   case 9 :
     console.log("A๋“ฑ๊ธ‰ ์ž…๋‹ˆ๋‹ค");
     break;
   case 8 :
     console.log("B๋“ฑ๊ธ‰ ์ž…๋‹ˆ๋‹ค");
     break;
   case 7 :
     console.log("C๋“ฑ๊ธ‰ ์ž…๋‹ˆ๋‹ค");
     break;
   case 6 :
     console.log("D๋“ฑ๊ธ‰ ์ž…๋‹ˆ๋‹ค");
     break;
   default :
     console.log("F๋“ฑ๊ธ‰ ์ž…๋‹ˆ๋‹ค");
 }
 // "B๋“ฑ๊ธ‰ ์ž…๋‹ˆ๋‹ค" ์ถœ๋ ฅ
switch (new Date().getDay()) {
  case 0:
    day = "Sunday";
    break;
  case 1:
    day = "Monday";
    break;
  case 2:
     day = "Tuesday";
    break;
  case 3:
    day = "Wednesday";
    break;
  case 4:
    day = "Thursday";
    break;
  case 5:
    day = "Friday";
    break;
  case 6:
    day = "Saturday";
}
// ํ˜„์žฌ ์˜ค๋Š˜ ์š”์ผ์„ ๋ฐ˜์˜ํ•˜์—ฌ ํ•ด๋‹นํ•˜๋Š” case์—์„œ break;
/*
class ์ƒ์„ฑ์„ ์—ฐ์Šตํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.
- MyMath๋ผ๋Š” class๋ฅผ ์ƒ์„ฑํ•ด์ฃผ์„ธ์š”.
- constructor์—์„œ๋Š” ์ˆซ์ž 2๊ฐœ๋ฅผ ์ธ์ž๋กœ ๋ฐ›์•„ ํ”„๋กœํผํ‹ฐ๋กœ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
- ์ด 4๊ฐœ์˜ ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•ด์ฃผ์„ธ์š”.
    - getNumber: ๋‘ ๊ฐœ์˜ ์ˆซ์ž๊ฐ€ ๋ฌด์—‡์ธ์ง€ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ ex) [1, 2]
    - add: ๋‘ ๊ฐœ์˜ ์ˆซ์ž๋ฅผ ๋”ํ•˜๋Š” ๋ฉ”์„œ๋“œ
    - substract: ๋‘ ๊ฐœ์˜ ์ˆซ์ž๋ฅผ ๋นผ๋Š” ๋ฉ”์„œ๋“œ
    - multiply: ๋‘ ๊ฐœ์˜ ์ˆซ์ž๋ฅผ ๊ณฑํ•˜๋Š” ๋ฉ”์„œ๋“œ
*/
class MyMath {
  constructor(a,b) {
    this.getNumber = [a,b],
    this.add = a+b,
    this.substract = a-b,
    this.multiply = a*b
  }
}

let result = new MyMath(3,2)
console.log(result)
// ์ถœ๋ ฅ๊ฒฐ๊ณผ : 
// MyMath {
//   getNumber: [ 3, 2 ],
//   add: 5,
//   substract: 1,
//   multiply: 6,
//   __proto__: MyMath { constructor: ฦ’ MyMath() }
// }

โœ… Array methods

๐Ÿ‘‰ arrow function์„ ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉํ•  ๋•Œ๋Š” call backํ•จ์ˆ˜๋กœ ์‚ฌ์šฉํ•  ๋•Œ์ด๋‹ค.
โ— ์ฝœ๋ฐฑํ•จ์ˆ˜๋ž€, ์ธ์ž๋กœ ์ „๋‹ฌ๋˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋œปํ•จ.

๐Ÿ‘‰ Array.map ( ์ฝœ๋ฐฑํ•จ์ˆ˜ )
map ๋ฉ”์†Œ๋“œ๋Š” ๋ฐฐ์—ด์„ ๋ฐ˜๋ณตํ•ด ์ค€๋‹ค. callbackํ•จ์ˆ˜์—์„œ returnํ•œ ๊ฐ’์œผ๋กœ ๊ฐ๊ฐ์˜ ์š”์†Œ๋ฅผ ์ˆ˜์ •ํ•ด์ค€๋‹ค.
map ๋งค์†Œ๋“œ์˜ return๊ฐ’์€ ์ˆ˜์ •๋œ ๊ฐ’์œผ๋กœ ๋‹ค์‹œ ์ƒ์„ฑ๋œ ๋ฐฐ์—ด์ด๋‹ค.

const arr = [1,2,3];
arr.map( (e) => {
	return e * e;
    }
)
// [1, 4, 9] ์ถœ๋ ฅ(๊ธฐ์กด arr์˜ ๋ฐฐ์—ด์€ ๊ทธ๋Œ€๋กœ ๋‘๊ณ , ์ƒˆ๋กœ์šด ๋ฐฐ์—ด ์ถœ๋ ฅ)

โ— Array ํƒ€์ž…์˜ ๋ฐ์ดํ„ฐ ์š”์†Œ ๊ฐฏ์ˆ˜ ๋งŒํผ ๋ฐ˜๋ณตํ•œ๋‹ค(๊ฐ๊ฐ์˜ ์š”์†Œ๋ฅผ ์ „๋ถ€ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด๋กœ return)
โ— ์ˆ˜์ •ํ•˜๊ณ  ์‹ถ์€๋Œ€๋กœ map ๋ฉ”์†Œ๋“œ ์•ˆ์— ๋กœ์ง์„ ๊ตฌํ˜„ํ•˜๊ณ  returnํ•ด์ฃผ๋ฉด ๋œ๋‹ค

๐Ÿ‘‰ Array.forEach ( ์ฝœ๋ฐฑํ•จ์ˆ˜ )
forEach๋Š” for๋Œ€์‹  ์‚ฌ์šฉํ•˜๋Š” ๋ฐ˜๋ณต๋ฌธ์ด๋‹ค.
map๊ณผ ๊ฐ€์žฅ ํฐ ์ฐจ์ด๋Š”, forEachํ•จ์ˆ˜ ์ž์ฒด๊ฐ€ returnํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋ƒฅ forEach ํ•จ์ˆ˜๋ฅผ ํƒˆ์ถœํ•˜๊ณ  ์‹ถ์„ ๋•Œ return์„ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.
๊ทธ๋ž˜์„œ forEach๋กœ ์ „๋‹ฌ๋˜๋Š” ์ฝœ๋ฐฑํ•จ์ˆ˜์—์„œ๋„ returnํ•˜๋Š” ๊ฒƒ์ด ์—†๋‹ค. ๋‹จ์ง€ for๋ฌธ ๋Œ€์‹  ์‚ฌ์šฉํ•˜๋Š” ๋ฐ˜๋ณต method์ผ ๋ฟ์ด๋‹ค.

let empty = [];
let names = ['a', 'ab', 'cbb', 'ada'];

names.forEach(el => {   
  if (el.includes('a')) {     
    empty.push(el);   
  } 
});
console.log(empty)
// ์ถœ๋ ฅ๊ฒฐ๊ณผ : [ 'a', 'ab', 'ada' ]
profile
boma91@gmail.com

2๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2021๋…„ 2์›” 9์ผ

์ €๋„ ์ƒˆ๋ด„๋‹˜์ฒ˜๋Ÿผ ๋งค์šฐ ๊น”๋”ํ•˜๊ณ  ์ƒ์„ธํžˆ ์ •๋ฆฌ ๋‹ค์‹œํ• ๋ž˜์š”...

1๊ฐœ์˜ ๋‹ต๊ธ€