๋ฐฐ์—ด๊ด€๋ จ ๋ฉ”์†Œ๋“œ๐Ÿ’ก


๐Ÿงก์›๋ณธ๋ฐฐ์—ด์— ์†์ƒ์„ ์ฃผ์ง€์•Š๋Š” ๋ฉ”์†Œ๋“œ

๐Ÿ’›.length

  • ํ•จ์ˆ˜์˜ ์ „์ฒด ๊ธธ์ด๋ฅผ ๋ฐ˜ํ™˜
  const numbers = [1,2,3,4]

  console.log(numbers.length) // 4

๐Ÿ’›.find(cb)

  • ์ฝœ๋ฐฑํ•จ์ˆ˜ ๋‚ด ์ฃผ์–ด์ง„ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์ฒซ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜
  const numbers = [1,2,3,4]

  console.log(numbers.find((num) => num > 2) //3
  // num > 2 ์˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์š”์†Œ๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ ์ด์ง€๋งŒ ์ฒซ๋ฒˆ์งธ ๊ฐ’๋งŒ ๋ฐ˜ํ™˜ ๋จ

๐Ÿ’›.findIndex(cb)

  • ์ฝœ๋ฐฑํ•จ์ˆ˜ ๋‚ด ์ฃผ์–ด์ง„ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์ฒซ๋ฒˆ์งธ ์š”์†Œ์˜ ์ธ๋ฑ์Šค๊ฐ’ ๋ฐ˜ํ™˜
  const numbers = [1,2,3,4]

  console.log(numbers.findIndex((num) => num > 2) //2
  // 2๋ณด๋‹ค ํฐ ์š”์†Œ ์ค‘ ์ฒซ๋ฒˆ์งธ ์š”์†Œ์ธ 3์˜ index๊ฐ’ => 2

๐Ÿ’›.includes()

  • ์ธ์ž๋กœ ์ฃผ์–ด์ง„ ๊ฐ’๊ณผ ์ผ์น˜ํ•˜๋Š” ์š”์†Œ๊ฐ€ ๋ฐฐ์—ด์— ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€์˜ ์—ฌ๋ถ€๋ฅผ boolean๋ฐ์ดํ„ฐ๋กœ ๋ฐ˜ํ™˜
  const numbers = [1,2,3,4]

  console.log(numbers.includes(3)) //true
  console.log(numbers.includes(6)) //false

๐Ÿ’›.concat()

  • ๋‘๊ฐœ์˜ ๋ฐฐ์—ด ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ‘ํ•ฉํ•˜์—ฌ ํ•˜๋‚˜์˜ ๋ฐ์ดํ„ฐ๋กœ ๋ฐ˜ํ™˜
  const abc = ['a', 'b', 'c']
  const def = ['d', 'e', 'f']

  console.log(abc.concat(def)) //['a', 'b', 'c', 'd', 'e', 'f']

๐Ÿ’›.forEach(cb)

  • ํ•ด๋‹น ๋ฐฐ์—ด์˜ ์š”์†Œ์˜ ๊ฐฏ์ˆ˜๋งŒํผ ์ฝœ๋ฐฑํ•จ์ˆ˜๊ฐ€ ๋ฐ˜๋ณต์‹คํ–‰๋จ
  • ๊ฐ item, ๊ฐ index๊ฐ’, ์ „์ฒด๋ฐฐ์—ด์ด ์ธ์ž๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค
  • ๋ฐ˜ํ™˜๋˜๋Š” ๊ฐ’์€ ์—†์Œ
  const fruits = ["Apple","Banana","Cherry"]

  fruits.forEach((item, idx, arr) => {
    console.log(item, idx, arr)
  });
  /* 
    ์ถœ๋ ฅ๊ฐ’
    Apple 0 [ 'Apple', 'Banana', 'Cherry' ]
    Banana 1 [ 'Apple', 'Banana', 'Cherry' ]
    Cherry 2 [ 'Apple', 'Banana', 'Cherry' ]
  */
  • ๊ฒฐ๊ณผ๊ฐ’์„ ๋ฐ˜ํ™˜ ํ•  ์ˆ˜ ์—†๊ธฐ๋•Œ๋ฌธ์— ๊ฐ’์„ returnํ•ด๋„ undefined๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค
  const fruits = ["Apple","Banana","Cherry"]

  const fruit = fruits.forEach((item) => {
    return item
  });

  console.log(fruit) //undefined

๐Ÿ’›.map(cb)

  • ํ•ด๋‹น ๋ฐฐ์—ด์˜ ๊ฐฏ์ˆ˜๋งŒํผ ์ฝœ๋ฐฑํ•จ์ˆ˜๊ฐ€ ๋ฐ˜๋ณต์‹คํ–‰๋จ
  • ๊ฐ item ๊ณผ index๊ฐ’์ด ์ธ์ž๋กœ ์ฃผ์–ด์ง
  • return๊ฐ’์„ ๋ฐฐ์—ด์˜ ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜
  const fruits = ["Apple","Banana","Cherry"]

  const a = fruits.map((item,idx) => (
    return {
    item: item, 
    index: idx
    }
  ))

  console.log(a)
  /* 
    ๋ฐ˜ํ™˜๊ฐ’
    [
      { item: 'Apple', index: 0 },
      { item: 'Banana', index: 1 },
      { item: 'Cherry', index: 2 }
    ]
  */

forEach()์™€ map()์˜ ๊ฐ€์žฅ ํฐ ์ฐจ์ด๋Š” returnํ•˜๋Š” ๊ฐ’์ด ์žˆ๋Š๋ƒ ์—†๋Š๋ƒ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋‹ค

๐Ÿ’›.reduce()

  • ๋Œ€์ƒ ๋ฐฐ์—ด์˜ ๊ธธ์ด๋งŒํผ ์ฃผ์–ด์ง„ ์ฝœ๋ฐฑ์„ ์‹คํ–‰ ํ•œ ๋’ค, ๋ˆ„์ ๊ฐ’์„ ๋ฐ˜ํ™˜
  • acc : ๋ˆ„์ ๊ฐ’
    cur : ํ˜„์žฌ ์š”์†Œ
const numbers = [1,2,3,4]

numbers.reduce((acc,cur) => acc + cur) // 10
// 1 + 2 + 3 + 4 = 10 ๋ฐ˜ํ™˜

๋ธ”๋กœ๊ทธ์— reduce์— ๋Œ€ํ•ด ๋” ์ž์„ธํžˆ ๊ธฐ์ˆ ํ•œ ํฌ์ŠคํŠธ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. Click!

๐Ÿ’›.filter(cb)

  • ์ฝœ๋ฐฑํ•จ์ˆ˜ ๋‚ด์˜ ์กฐ๊ฑด์‹์— true๋กœ ๋Œ€์‘ํ•˜๋Š” ์š”์†Œ๋งŒ ๋ฐฐ์—ด์— ๋‹ด์•„ ๋ฐ˜ํ™˜
  const numbers = [1,2,3,4]

  //map์˜ ๊ฒฝ์šฐ ๋น„๊ต์—ฐ์‚ฐ์ž๋กœ ์กฐ๊ฑด์‹ ์ž‘์„ฑ ์‹œ boolean๋ฐ์ดํ„ฐ๋กœ ๊ฐ’์ด ๋ฐ˜ํ™˜ ๋จ
  const b = numbers.map((num) => num < 3) 
  console.log(b) //[ true, true, false, false ]

  //filter์˜ ๊ฒฝ์šฐ true๊ฐ’์œผ๋กœ ๋Œ€์‘ํ•˜๋Š” ์š”์†Œ๋ฅผ ๋ฐฐ์—ด์— ๋‹ด์•„ ๋ฐ˜ํ™˜
  const c = numbers.filter((num) => num < 3) 
  console.log(c) //[ 1, 2 ]

๐Ÿ’›.every()

  • ๋ชจ๋“ ย ์กฐ๊ฑด์„ย ๋งŒ์กฑํ•ด์•ผ๋งŒย true๋ฅผย return
  const arr = [1,2,3,4,5]
  
  arr.every((item) => item > 3) // false
  arr.every((item) => item < 6) //true

๐Ÿ’›.some()

  • ํ•˜๋‚˜์˜ย ์กฐ๊ฑด๋งŒย ๋งŒ์กฑํ•ด๋„ย true๋ฅผย ย return
  const arr = [1,2,3,4,5]
  
  arr.some((item) => item > 3) // true
  arr.some((item) => item > 6) // false

๐Ÿ’›Array.from(), Array.isArray()

  • Array.from() : ์œ ์‚ฌ๋ฐฐ์—ด์„ ์‹ค์ œ ๋ฐฐ์—ด๋กœ ๋ฐ”๊ฟ”์ฃผ๋Š” ์ •์ ๋ฉ”์†Œ๋“œ
  • Array.isArray() : ํ•ด๋‹น ์š”์†Œ๊ฐ€ ๋ฐฐ์—ด์ธ์ง€ ์•„๋‹Œ์ง€ boolean๊ฐ’์„ ๋ฐ˜ํ™˜
// querySelectorAll() ๋กœ ์„ ํƒํ•ด ์˜จ ์š”์†Œ๋“ค์€ ์œ ์‚ฌ๋ฐฐ์—ด
const liEls = document.querySelectorAll('li');

console.log(liEls); //NodeList(8) [li, li, li, li, li, li, li, li]
console.log(Array.isArray(liEls)) //false


// Array.from() ์„ ํ†ตํ•ด ์‹ค์ œ ๋ฐฐ์—ด๋กœ ์ „ํ™˜
const arrLiEls = Array.from(liEls);

console.log(arrLiEls); //Array(8) [li, li, li, li, li, li, li, li]
console.log(Array.isArray(arrLiEls)) //true

๐Ÿงก์›๋ณธ ๋ฐฐ์—ด์ด ์ˆ˜์ •๋˜๋Š” ๋ฉ”์†Œ๋“œ

๐Ÿ’›.push()

  • ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ ์š”์†Œ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฝ์ž…
  const numbers = [1,2,3,4]

  numbers.push(5)

  console.log(numbers) // [1, 2, 3, 4, 5]

๐Ÿ’›.pop()

  • ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ๊ทธ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜
  const numbers = [1,2,3,4]

  console.log(numbers.pop()) //4
  console.log(numbers) // [1, 2, 3]

๐Ÿ’›.shift()

  • ๋ฐฐ์—ด์˜ ์ฒซ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ๊ทธ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜
  const numbers = [1,2,3,4]

  console.log(numbers.pop()) //1
  console.log(numbers) // [2, 3, 4]

๐Ÿ’›.unshift()

  • ๋ฐฐ์—ด์˜ ์ฒซ๋ฒˆ์งธ ์š”์†Œ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฝ์ž…
  const numbers = [1,2,3,4]

  numbers.unshift(0)

  console.log(numbers) // [0, 1, 2, 3, 4]

๐Ÿ’›.sort()

  • ๋Œ€์ƒ ๋ฐฐ์—ด์„ ์ฝœ๋ฐฑ์˜ ๋ฐ˜ํ™˜๊ฐ’์— ๋”ฐ๋ผ ์ •๋ ฌ
  • ๋‘๊ฐœ์˜ ์ธ์ž ์ œ๊ณต. (์ด์ „์š”์†Œ, ํ˜„์žฌ์š”์†Œ)
  • ์ฝœ๋ฐฑ์„ ์ œ๊ณตํ•˜์ง€ ์•Š์œผ๋ฉด ์š”์†Œ๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•˜๊ณ  ์œ ๋‹ˆ์ฝ”๋“œ ์ฝ”ํŠธ ํฌ์ธํŠธ ์ˆœ์„œ๋กœ ์ •๋ ฌ
const numbers  = [4, 17, 21, 103, 1, 0, 78]

// ์œ ๋‹ˆ์ฝ”๋“œ ํฌ์ธํŠธ ์ˆœ์„œ ์ •๋ ฌ (์ˆซ์ž ์•ž์ž๋ฆฌ ํฌ๊ธฐ ์ˆœ)
numbers.sort()
console.log(numbers) // [0, 1, 103, 17, 21, 4, 78]

//a - b ์˜ ๊ฒฐ๊ณผ๊ฐ’์ด ์Œ์ˆ˜๋ฉด a๊ฐ€ ๋” ํฐ ์ˆ˜ , ์–‘์ˆ˜๋ฉด b๊ฐ€ ๋” ํฐ์ˆ˜ ๋ผ๋Š” ๋ฒ•์น™์„ ํ†ตํ•ด ์ˆซ์ž๋ฅผ ํฌ๊ธฐ์ˆœ ์ •๋ ฌ
numbers.sort((a,b) => a - b)
console.log(numbers) // [0, 1, 4, 17, 21, 78, 103]

// b - a๋กœ ๋’ค์ง‘์œผ๋ฉด ํฐ์ˆ˜๋ถ€ํ„ฐ ์ž‘์€ ์ˆ˜ ์ˆœ์„œ๋กœ ์ •๋ ฌ
numbers.sort((a, b) => b - a)
console.log(numbers) // [103, 78, 21, 17, 4, 1, 0]

๐Ÿ’›.reverse()

  • ๋ฐฐ์—ด๋ฐ์ดํ„ฐ๋ฅผ ๊ฑฐ๊พธ๋กœ ๋’ค์ง‘์–ด์คŒ
  const numbers = [1,2,3,4]

  numbers.reverse()

  console.log(numbers) //[4, 3, 2, 1]

๐Ÿ’›.splice()

  • ๋ฐฐ์—ด์„ ์›ํ•˜๋Š” ์œ„์น˜(์ธ๋ฑ์Šค)์—์„œ๋ถ€ํ„ฐ ์›ํ•˜๋Š” ๊ธธ์ด๋งŒํผ ์‚ญ์ œ
  • ์ตœ์†Œ 3๊ฐœ์˜ ์ธ์ž๋ฅผ ๋„ฃ์„ ์ˆ˜ ์žˆ์Œ
  • ์ฒซ๋ฒˆ์งธ ์ธ์ž : ์‹œ์ž‘ index
  • ๋‘๋ฒˆ์งธ ์ธ์ž : ์‚ญ์ œํ•  ์š”์†Œ์˜ ๊ฐฏ์ˆ˜
  • ์„ธ๋ฒˆ์งธ ์ธ์ž : ํ•ด๋‹น ์ž๋ฆฌ์— ์ถ”๊ฐ€ ํ•  ๊ฐ’ (์˜ต์…”๋„. ์ƒ๋žต๊ฐ€๋Šฅ)
  • ์œ„์—์„œ ์ตœ์†Œ 3๊ฐœ์˜ ์ธ์ž๋ฅผ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ–ˆ๋Š”๋ฐ, ์„ธ๋ฒˆ์งธ ์ธ์ž ์ดํ›„๋กœ๋Š” ๋ชจ๋‘ ์ถ”๊ฐ€ํ•  ๊ฐ’์œผ๋กœ ์ธ์‹๋จ
  const numbers = [1,2,3,4]

  //๊ธฐ๋ณธ ์‚ฌ์šฉ๋ฒ•
  numbers.splice(2,1); //2๋ฒˆ ์•„์ดํ…œ๋ถ€ํ„ฐ 1๊ฐœ์˜ ์•„์ดํ…œ ์‚ญ์ œ
  console.log(numbers) //[ 1, 2, 4 ]


  //ํ˜„์žฌ numbers ์˜ ๊ฐ’ = [ 1, 2, 4 ]
  //์‚ญ์ œํ•  ์š”์†Œ์˜ ๊ฐฏ์ˆ˜๋ฅผ 0์œผ๋กœ ์ง€์ •ํ•˜๋ฉด ์‚ญ์ œ๋˜๋Š” ์š”์†Œ ์—†์ด ์•„์ดํ…œ ์ถ”๊ฐ€ ๊ฐ€๋Šฅ
  numbers.splice(2, 0, 3); //2๋ฒˆ ์•„์ดํ…œ๋ถ€ํ„ฐ 0๊ฐœ์˜ ์•„์ดํ…œ ์‚ญ์ œ, 3 ์ถ”๊ฐ€
  console.log(numbers) //[ 1, 2, 3, 4 ]


  //ํ˜„์žฌ numbers ์˜ ๊ฐ’ = [ 1, 2, 3, 4 ]
  //ํ•˜๋‚˜์˜ ์•„์ดํ…œ์„ ์ œ๊ฑฐํ•˜๊ณ  ํ•˜๋‚˜์˜ ์•„์ดํ…œ์„ ์ถ”๊ฐ€ํ•˜์—ฌ ํ•ด๋‹น ์š”์†Œ๋ฅผ ๊ต์ฒด
  numbers.splice(2, 1, 0); //2๋ฒˆ ์•„์ดํ…œ๋ถ€ํ„ฐ 1๊ฐœ์˜ ์•„์ดํ…œ ์‚ญ์ œ, 0 ์ถ”๊ฐ€
  console.log(numbers) //[ 1, 2, 0, 4 ]

๋งˆ์น˜๋ฉฐ๐Ÿ™Œ

์•Œ๊ณ ๋Š” ์žˆ์ง€๋งŒ ์ต์ˆ™ํ•˜์ง€ ์•Š์•„์„œ ์‚ฌ์šฉํ•ด ๋ณธ ์ ์ด ์—†์—ˆ๋˜ ๋ฉ”์†Œ๋“œ๋“ค์€ ์ด๋ฒˆ ๊ธฐํšŒ์— ๋‹ค์‹œํ•œ๋ฒˆ ์‚ฌ์šฉ๋ฒ•์„ ์ตํžˆ๋ฉฐ ๋ฆฌ๋งˆ์ธ๋“œ ๋˜์—ˆ๊ณ , ์ผ๋ถ€ ๋ฉ”์„œ๋“œ๋“ค์€ ์ตํžˆ ์‚ฌ์šฉํ•˜๋˜ ๊ฒƒ์ž„์—๋„ ์ƒˆ๋กญ๊ฒŒ ๋‹ค๊ฐ€์™”๋‹ค.
ํŠนํžˆ forEach์™€ map๊ฐ€ ์–ด๋–ค ์ƒํ™ฉ์—์„œ ์“ฐ์ด๋Š”์ง€ ๊ทธ ์ฐจ์ด๋ฅผ ๊ทธ๋ƒฅ ๋Š๋‚Œ์ (..)์œผ๋กœ๋งŒ ์•Œ๊ณ  ์žˆ์—ˆ๋Š”๋ฐ ์ •ํ™•ํ•œ ์ฐจ์ด์ ์„ ์ฒ˜์Œ์œผ๋กœ ์ธ์ง€ํ–ˆ๋‹ค...!!
๊ฐœ๋ฐœ์ž์—๊ฒŒ ์™œ? ์–ด๋–ป๊ฒŒ? ๋ผ๋Š” ๊ถ๊ธˆ์ฆ์€ ๊ณ„์†ํ•ด์„œ ํ’ˆ๊ณ ๋‹ค๋…€์•ผ ํ•˜๋Š” ์งˆ๋ฌธ์ธ๊ฒƒ์„ ๊ฐ„๊ณผํ•˜๊ณ , ๋Š๋‚Œ์ ์ธ ๋Š๋‚Œ์œผ๋กœ๋งŒ ์ตํžˆ๊ณ  ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์—ˆ๋˜ ๊ฒƒ์€ ์•„๋‹๊นŒ..ํ•˜๋ฉฐ ๋‚˜๋ฅผ ๋˜๋Œ์•„๋ณด๊ฒŒ ๋œ๋‹ค...๐Ÿ˜ถโ€๐ŸŒซ๏ธ

profile
ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ์ž ์„ฑ์žฅ์ผ๊ธฐ ๐Ÿ’ญ

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