๐Ÿ“ [JS ๊ณต๋ถ€๊ธฐ๋ก] ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ฐ์ดํ„ฐ์™€ ์•Œ์•„์•ผํ•  ๊ฐœ๋…๋“ค

์ˆซ์ž์™€ ์ˆ˜ํ•™

  • toFixed() โ†’ ์†Œ์ˆ˜์  ์ž๋ฆฌ์ˆ˜
  • parseInt(str) โ†’ ์ˆซ์ž๋ฅผ ์ •์ˆ˜๋กœ ์ถ”์ถœ
  • parseFloat(str) โ†’ ์ˆซ์ž๋ฅผ ์‹ค์ˆ˜๋กœ ์ถ”์ถœ

Math ๋‚ด์žฅ ๊ฐ์ฒด

  • Math.abs() โ†’ ์ฃผ์–ด์ง„ ์ˆซ์ž์˜ ์ ˆ๋Œ€๊ฐ’์„ ๋ฐ˜ํ™˜
  • Math.min(2, 8) โ†’ ์ฃผ์–ด์ง„ ์ˆซ์ž์˜ ์ตœ์†Œ๊ฐ’ ๋ฐ˜ํ™˜
  • Math.max(2, 8) โ†’ ์ฃผ์–ด์ง„ ์ˆซ์ž์˜ ์ตœ๋Œ€๊ฐ’ ๋ฐ˜ํ™˜
  • Math.ceil(3.14) โ†’ ์ฃผ์–ด์ง„ ์ˆซ์ž๋ฅผ ์˜ฌ๋ฆผ์ฒ˜๋ฆฌ
  • Math.floor(3.14) โ†’ ์ฃผ์–ด์ง„ ์ˆซ์ž๋ฅผ ๋‚ด๋ฆผ์ฒ˜๋ฆฌ
  • Math.round(3.14) โ†’ ์ฃผ์–ด์ง„ ์ˆซ์ž๋ฅผ ๋ฐ˜์˜ฌ๋ฆผ์ฒ˜๋ฆฌ
  • Math.random() โ†’ ๋žœ๋คํ•œ ์ˆซ์ž ๋ฐ˜ํ™˜

๋ฐฐ์—ด API

Array.prototype

  • .find()
    const array1 = [5, 12, 8, 130, 44]
    
    const found = array1.find(el => el > 10)
    
    console.log(found)
  • .length - ๋ฐฐ์—ด์˜ ๊ธธ์ด๋ฅผ ์•Œ๋ ค์ฃผ๋Š” ์†์„ฑ
  • .concat() - ๋‘๊ฐœ์˜ ๋ฐฐ์—ด ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ‘ํ•ฉํ•˜์—ฌ ๋ฐ˜ํ™˜(์›๋ณธ ๋ฐ์ดํ„ฐ ์†์ƒX)
  • .forEach() - ๋ฐฐ์—ด์˜ ์›์†Œ ๊ฐœ์ˆ˜๋งŒํผ ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ์‹คํ–‰
    let fruits = ['Apple', 'Banana', 'Pineapple']
    
    fruits.forEach(elements, index, array) {
    	console.log(elements, index, array)
    }
  • .map() - ๋ฐฐ์—ด ๋‚ด์˜ ๋ชจ๋“  ์š”์†Œ ๊ฐ๊ฐ์— ๋Œ€ํ•˜์—ฌ ์ฃผ์–ด์ง„ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ชจ์•„ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด ๋ฐ˜ํ™˜(๋ณธ๋ž˜์˜ ๋ฐฐ์—ด๊ณผ ๊ธธ์ด๊ฐ€ ๊ฐ™์Œ!)
    const b = fruits.map((fruit, i) => ({
    	id: i,
    	name: fruit
    }))
  • .filter() - ์ฃผ์–ด์ง„ ํ•จ์ˆ˜์˜ ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜๋Š” ๋ชจ๋“  ์š”์†Œ๋ฅผ ๋ชจ์•„ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜
    const numbers = [1, 2, 3, 4]
    const fruits = ['Apple', 'Banana', 'Cherry']
    
    const a = fruits.filter(number => number > 3)
  • (๊ฐ™์€ ๋กœ์ง์„ map ์ ์šฉ์‹œ boolean ๋ฐ์ดํ„ฐ๋กœ ๋‚˜์˜ด!)
  • .find() - ์ฃผ์–ด์ง„ ํŒ๋ณ„ ํ•จ์ˆ˜๋ฅผ ๋งŒ์กฑํ•˜๋Š” ์ฒซ ๋ฒˆ์งธ ์š”์†Œ์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜, ๊ทธ๋Ÿฐ ์š”์†Œ๊ฐ€ ์—†๋‹ค๋ฉด undefined ๋ฐ˜ํ™˜
    const numbers = [1, 2, 3, 4]
    const fruits = ['Apple', 'Banana', 'Cherry']
    
    const a = fruits.find(fruit => /^B/.test(fruit))
  • .indeIndex() - ์ฃผ์–ด์ง„ ํŒ๋ณ„ ํ•จ์ˆ˜๋ฅผ ๋งŒ์กฑํ•˜๋Š” ์ฒซ ๋ฒˆ์งธ ์š”์†Œ์˜ ์ธ๋ฑ์Šค ๋ฐ˜ํ™˜, ์—†๋‹ค๋ฉด -1 ๋ฐ˜ํ™˜
    const numbers = [1, 2, 3, 4]
    const fruits = ['Apple', 'Banana', 'Cherry']
    
    const a = fruits.findIndex((fruit => return /^B/.test(fruit))
  • .includes() - ๋ฐฐ์—ด์ด ํŠน์ • ์š”์†Œ๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š”์ง€ ํŒ๋ณ„
    const numbers = [1, 2, 3, 4]
    const fruits = ['Apple', 'Banana', 'Cherry']
    
    const a = numbers.includes(3)
    console.log(a) // true

์›๋ณธ ์ˆ˜์ •๋จ!

  • .push() - ๋ฐฐ์—ด์˜ ๋์— ํ•˜๋‚˜ ์ด์ƒ์˜ ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ , ๋ฐฐ์—ด์˜ ์ƒˆ๋กœ์šด ๊ธธ์ด๋ฅผ ๋ฐ˜ํ™˜
    const animals = ['pigs', 'goats', 'sheep']
    
    const count = animals.push('cows')
    console.log(count)
    // 4
    
    console.log(animals)
    // Array ['pigs', 'goats', 'sheep', 'cows']
  • .unshift() - ์ƒˆ๋กœ์šด ์š”์†Œ๋ฅผ ๋ฐฐ์—ด์˜ ๋งจ ์•ž์ชฝ์— ์ถ”๊ฐ€ํ•˜๊ณ , ์ƒˆ๋กœ์šด ๊ธธ์ด๋ฅผ ๋ฐ˜ํ™˜
    const arr1 = [1, 2, 3]
    
    console.log(arr1.unshift(4, 5))
    // 5
    
    console.log(arr1)
    // Array [4, 5, 1, 2, 3]
  • .reverse() - ๋ฐฐ์—ด์˜ ์ˆœ์„œ๋ฅผ ๋ฐ˜์ „
    const numbers = [1, 2, 3, 4]
    const fruits = ['Apple', 'Banana', 'Cherry']
    
    numbers.reverse()
    fruits.reverse()
  • .splice() - ๋ฐฐ์—ด์˜ ๊ธฐ์กด ์š”์†Œ๋ฅผ ์‚ญ์ œ ๋˜๋Š” ๊ต์ฒดํ•˜๊ฑฐ๋‚˜ ์ƒˆ ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ๋ฐฐ์—ด์˜ ๋‚ด์šฉ์œผ๋กœ ๋ณ€๊ฒฝ
    • ์ธ์ˆ˜๊ฐ€ 2๊ฐœ โ†’ .splice(2, 1) : ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ 2๋ฒˆ๋ถ€ํ„ฐ 1๊ฐœ์˜ ๋ฐ์ดํ„ฐ ์ง€์›€

    • ์ธ์ˆ˜๊ฐ€ 3๊ฐœ โ†’ .splice(2, 1, 99) : ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ 2๋ฒˆ๋ถ€ํ„ฐ 1๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ง€์šฐ๊ณ  99 ์ถ”๊ฐ€

      const months = ['Jan', 'March', 'April', 'June'];
      months.splice(1, 0, 'Feb');
      // inserts at index 1
      console.log(months);
      // expected output: Array ["Jan", "Feb", "March", "April", "June"]
      
      months.splice(4, 1, 'May');
      // replaces 1 element at index 4
      console.log(months);
      // expected output: Array ["Jan", "Feb", "March", "April", "May"]

๊ฐ์ฒด(Object)

์ •์  ๋ฉ”์†Œ๋“œ โ†’ ๊ฐ์ฒด ํ”„๋กœํ† ํƒ€์ž…์— ์ด๋ฏธ ๋“ฑ๋ก๋˜์–ด ์žˆ๋Š” ํ•จ์ˆ˜๋“ค

  • ๋ฆฌํ„ฐ๋Ÿด ๋ฐฉ์‹์œผ๋กœ ๋ฐ”๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Œ

  • Object.assign(๋Œ€์ƒ๊ฐ์ฒด, ์ถœ์ฒ˜๊ฐ์ฒด) - ์—ด๊ฑฐํ•  ์ˆ˜ ์žˆ๋Š” ํ•˜๋‚˜ ์ด์ƒ์˜ ์ถœ์ฒ˜ ๊ฐ์ฒด๋กœ๋ถ€ํ„ฐ ๋Œ€์ƒ ๊ฐ์ฒด๋กœ ์†์„ฑ์„ ๋ณต์‚ฌํ•  ๋•Œ ์‚ฌ์šฉ / ์ถœ์ฒ˜๊ฐ์ฒด ๋ฐ์ดํ„ฐ๋ฅผ ๋Œ€์ƒ๊ฐ์ฒด๋กœ ๋ณต์‚ฌํ•ด์„œ ๋ณ‘ํ•ฉ
const target = { a: 1, b: 2 };
const source = { b: 4, c: 5 };

const returnedTarget = Object.assign(target, source);

console.log(target);
// expected output: Object { a: 1, b: 4, c: 5 }

console.log(returnedTarget);
// expected output: Object { a: 1, b: 4, c: 5 }
const userAge = {
	name: 'zerone',
	age: 85
}

const userEmail = {
	name: 'zerone'
	email: '[email protected]'
}

const target = Object.assign({}, userAge, userEmail)

console.log(target === userAge) //flase
  • Object.keys() - ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์˜ ํ‚ค๋ฅผ ์ถ”์ถœํ•ด์„œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜

๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น (Destructuring assignment)

  • ๋ฐฐ์—ด์ด๋‚˜ ๊ฐ์ฒด์˜ ์†์„ฑ์„ ํ•ด์ฒดํ•˜์—ฌ ๊ทธ ๊ฐ’์„ ๊ฐœ๋ณ„ ๋ณ€์ˆ˜์— ๋‹ด์„ ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๋Š” ํ‘œํ˜„์‹
let a, b, rest;
[a, b] = [10, 20];

console.log(a);
// expected output: 10

console.log(b);
// expected output: 20

[a, b, ...rest] = [10, 20, 30, 40, 50];

console.log(rest);
// expected output: Array [30,40,50]

const fruits = ['Apple', 'Banana', 'Cherry']
const [ , , c] = fruits
// output: Cherry
const user = {
	name: 'zerone',
	age: 98.
	email: kc9994@naver.com
}

const {name, age: ๋‚˜์ด, address='korea', email} = user
// age๋ฅผ ๋‚˜์ด๋ผ๋Š” ๋ณ€์ˆ˜๋กœ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Œ

์ „๊ฐœ ์—ฐ์‚ฐ์ž(Spread)

  • ๋ฐฐ์—ด์ด๋‚˜ ๋ฌธ์ž์—ด๊ณผ ๊ฐ™์ด ๋ฐ˜๋ณต ๊ฐ€๋Šฅํ•œ ๋ฌธ์ž๋ฅผ 0๊ฐœ ์ด์ƒ์˜ ์ธ์ˆ˜ ๋˜๋Š” ์š”์†Œ๋กœ ํ™•์žฅ
function sum(x, y, z) {
  return x + y + z;
}

const numbers = [1, 2, 3];

console.log(sum(...numbers));
// expected output: 6
const fruits = ['Apple', 'Banana', 'Cherry', 'Orange']

function toObject(a, b, ...c) {
	return {
		a: a,
		b: b,
		c: c 
	}
}

console.log(toObject(...fruits)) // c ์—๋Š” Cherry์™€ Orange๊ฐ€ ๋ชจ๋‘ ๋‹ด๊ฒจ์žˆ์Œ

// ์ถ•์•ฝํ˜•์œผ๋กœ ์ž‘์„ฑ๊ฐ€๋Šฅ
const toObject = (a, b, ...c) => ({a, b, c})

๋ฐ์ดํ„ฐ ๋ถˆ๋ณ€์„ฑ(Immutability)

  • ์›์‹œ ๋ฐ์ดํ„ฐ: String, Number, Boolean, undefined, null ...
  • ์ฐธ์กฐํ˜• ๋ฐ์ดํ„ฐ: Array, Object, Function ...

์›์‹œํ˜• ๋ฐ์ดํ„ฐ๋Š” ๋ถˆ๋ณ€์„ฑ์„ ๊ฐ€์ง

let a = 1 // ์ฒซ ๋ฒˆ์งธ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ์— 1์ด ํ• ๋‹น
let b = 4 // ๋‘ ๋ฒˆ์งธ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ์— 4๊ฐ€ ํ• ๋‹น
console.log(a, b, a === b)
// 1 4 false
b = a // b๊ฐ€ ์ฐธ์กฐํ•˜๋Š” ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๋ฅผ a๊ฐ€ ์ฐธ์กฐํ•˜๋Š” ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๋กœ ๋ณ€๊ฒฝ
console.log(a, b, a === b)
// 1 1 true
a = 7 // ์„ธ ๋ฒˆ์งธ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ์— 7์ด ํ• ๋‹น
console.log(a, b, a === b)
// 7 1 false
let c = 1 // ์ฒซ ๋ฒˆ์งธ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๋ฅผ ์ฐธ์กฐ(๋„ค ๋ฒˆ์งธ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ์— 1์„ ํ• ๋‹นํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹˜)
console.log(b, c, b === c)
// 1 1 true

์ฐธ์กฐํ˜• ๋ฐ์ดํ„ฐ๋Š” ๊ฐ€๋ณ€์„ฑ์„ ๊ฐ€์ง(๋ถˆ๋ณ€์„ฑX?)

โ†’ ์ƒ๊ธด ๊ฒƒ์ด ๋˜‘๊ฐ™์•„๋„ ์„œ๋กœ ๊ฐ™์€ ๋ฐ์ดํ„ฐ๊ฐ€ ์•„๋‹ ์ˆ˜ ์žˆ๋‹ค

  • ํ• ๋‹น์‹œ ๋ฉ”๋ชจ๋ฆฌ ์ฐธ์กฐ ์ฃผ์†Œ๊ฐ€ ๋ฐ”๋€Œ๋ฉด์„œ ๊ฐ€๋ณ€์„ฑ์„ ๊ฐ€์ง
let a = {k: 1}
let b = {k: 1}
console.log(a, b, a === b)
// {k: 1} {k: 1} flase

a.k = 7
b = a
console.log(a, b, a === b)
// {k: 7} {k: 7} true

a.k = 2
console.log(a, b, a === b)
// {k: 2} {k: 2} true

let c = b // b ์˜ ๊ฐ’์ด c ๋กœ ๋ณต์‚ฌ๋˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ, ๊ฐ™์€ ์ฃผ์†Œ๋ฅผ ์ฐธ์กฐํ•œ๋‹ค๋Š” ๊ฒƒ!
console.log(a, b, c, a === c)
/// {k: 2} {k: 2} {k: 2} true

a.k = 9
console.log(a, b, c, a === c)
// {k: 9} {k: 9} {k: 9} true
// a ์˜ k ๊ฐ’๋งŒ ๋ฐ”๊ฟจ๋Š”๋ฐ b, c ๋˜ํ•œ a ์™€ ๊ฐ™์€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ฐธ์กฐํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ™์ด ๋ฐ”๋€œ
  • a = b ๋กœ ํ• ๋‹นํ•  ๋•Œ a ์™€ b๋ฅผ ์™„์ „ํžˆ ๊ตฌ๋ถ„ํ•ด์„œ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋ณต์‚ฌ๋ฅผ ํ†ตํ•ด ํ•  ์ˆ˜ ์žˆ์Œ(์–•์€๋ณต์‚ฌ, ๊นŠ์€๋ณต์‚ฌ)

์–•์€ ๋ณต์‚ฌ(Shallow copy), ๊นŠ์€ ๋ณต์‚ฌ(Deep copy)

์–•์€ ๋ณต์‚ฌ โ†’ (๊ฐ์ฒดโ€™๋งŒ' ๋ณต์‚ฌ)

1. Object.assgin()

const user = {
	name: 'zerone',
	age: 98,
	emails: '[email protected]'
}

**const copyUser = Object.assing({}, user)**

console.log(copyUser === user)
// false

user.age = 25
console.log('user', user)
// {name: 'zerone', age: 25, emails: '[email protected]'}
console.log('copyUser', copyUser)
// {name: 'zerone', age: 98, emails: '[email protected]'}

2. ์ „๊ฐœ ์—ฐ์‚ฐ์ž

const user = {
	name: 'zerone',
	age: 98,
	emails: '[email protected]'
}

**const copyUser = {...user}**

console.log(copyUser === user)
// false

user.age = 25
console.log('user', user)
// {name: 'zerone', age: 25, emails: '[email protected]'}
console.log('copyUser', copyUser)
// {name: 'zerone', age: 98, emails: '[email protected]'}

๊นŠ์€ ๋ณต์‚ฌ โ†’ (๊ฐ์ฒด ๋‚ด๋ถ€ ํ”„๋กœํผํ‹ฐ๋„ ๋ณต์‚ฌ)

1. Lodash

import _ from 'lodash' // ์–ธ๋”๋ฐ”(_) ๊ธฐํ˜ธ๊ฐ€ ํ•˜๋‚˜์˜ ๊ฐ์ฒด ๋ฐ์ดํ„ฐ

const user = {
	name: 'zerone',
	age: 98,
	emails: ['[email protected]']
}

const copyUser = _.cloneDeep(user)
console.log(copyUser === user)
// false

user.age = 25
// console.log('user', user)
// console.log('copyUser', copyUser)

user.emails.push('[email protected]')

console.log('user', user)
// {name: 'zerone', age: 25, emails: Array(2)}
console.log('copyUser', copyUser)
// {name: 'zerone', age: 98, emails: Array(1)}
  • _.clonseDeep()
    • ์žฌ๊ท€์ ์œผ๋กœ ๋ฐฐ์—ด์ด๋‚˜ ๊ฐ์ฒด ๋‚ด๋ถ€์˜ ๋ชจ๋“  ๊ฒƒ์„ ๋ณต์‚ฌ

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ