[TIL] Day11 #๋ฐฐ์—ด

Beanxxยท2022๋…„ 5์›” 10์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
11/120
post-thumbnail

[TIL] Day11
[SEB FE] Day11

โœ…ย ๋ฐฐ์—ด

: ์ˆœ์„œ๊ฐ€ ์žˆ๋Š” ๊ฐ’ / ๋Œ€๊ด„ํ˜ธ(square bracket, []) ์ด์šฉ

  • index: ์ˆœ์„œ / 1์ด ์•„๋‹Œ 0๋ถ€ํ„ฐ ๋ฒˆํ˜ธ๋ฅผ ๋งค๊น€
  • element(์š”์†Œ): index์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’ โ† ๊ฐ’์€ index๋ฅผ ์ด์šฉํ•ด ์ ‘๊ทผ
    • ๊ฐ๊ฐ์˜ element๋Š” ์‰ผํ‘œ(comma)๋กœ ๊ตฌ๋ถ„
      let myNumber = [10, 20, 30, 40];
      
      // index๊ฐ€ 3์ธ ๊ฐ’ ๋ณ€๊ฒฝ
      myNumber[3] = 400;
      myNumber;  // [10, 20, 30, 400];
      
      myNumber.length;  // 4. <- ๋ฐฐ์—ด์˜ ๊ธธ์ด
      
      myNumber.push(50);  // ๋ฐฐ์—ด ๋์— 50์ด๋ผ๋Š” ๊ฐ’์„ ์ถ”๊ฐ€
      myNumber;  // [10, 20, 30, 40, 50];
      
      myNumber.pop();  // ๋ฐฐ์—ด ๋งˆ์ง€๋ง‰ ๊ฐ’ ์‚ญ์ œ
      myNumber;  // [10, 20, 30, 40];
      
      myNumber[5];  // ๋ฐฐ์—ด์— ์—†๋Š” index -> undefined
      
      let yourNumber = [[10, 20], [30, 40], [50, 60]];
      yourNumber[1][0];  // 30
    โœ‹ย ์˜จ์ (dot)์„ ์ด์šฉํ•ด์„œ ๋ณ€์ˆ˜๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์†์„ฑ(property)์— ์ ‘๊ทผ ๊ฐ€๋Šฅ
    myNumber.length;
    โœ‹ย ์˜จ์ (dot)์„ ์ด์šฉํ•ด์„œ ๋ช…๋ น(method) ์‹คํ–‰ ๊ฐ€๋Šฅ (ํ•จ์ˆ˜ ์‹คํ–‰์ฒ˜๋Ÿผ () ํ˜•ํƒœ๋กœ ์‹คํ–‰)
    myNumber.push();
    โœ‹ย ๋ฐฐ์—ด์˜ ์œ ํšจํ•œ index๋ฅผ ๋ฒ—์–ด๋‚˜๋Š” ๊ฒฝ์šฐ undefined ์ถœ๋ ฅ

๐Ÿ“Žย ๋ฐฐ์—ด ๋ฐ˜๋ณต

  1. ๋ฐ˜๋ณต๋ฌธ์„ ์ด์šฉํ•˜์—ฌ ๋ฐฐ์—ด์˜ ์š”์†Œ(element)๋ฅผ ํ•œ๋ฒˆ์”ฉ ์ถœ๋ ฅ

    let myNum = [10, 20, 30, 40];
    
    for(let n = 0; n < myNum.length; n++) {
    	console.log(myNum[n]);
    }
  1. ๋ฐ˜๋ณต๋ฌธ์„ ์ด์šฉํ•˜์—ฌ ๋ฐฐ์—ด์˜ ๋ชจ๋“  element ๊ฐ’์˜ ํ•ฉ ์ถœ๋ ฅ

    let myNum = [10, 20, 30, 40];
    let sum = 0;
    
    for(let n = 0; n < myNum.length; n++) {
    	sum = sum + myNum[n];  // sum += myNum[n];
    }

๐Ÿ“Žย ๋ฐฐ์—ด ๋ฉ”์„œ๋“œ

  • Array.isArray(): ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ํŠน์ • ๊ฐ’์ด ๋ฐฐ์—ด์ธ์ง€ ์•„๋‹Œ์ง€ ํŒ๋ณ„
    let myNum = [10, 20, 30, 40];
    
    typeof myNum;  // "object" (๋ฐฐ์—ด์˜ ํƒ€์ž…)
    
    Array.isArray(myNum);  // true (๋ฐฐ์—ด์ด๋ฏ€๋กœ)
    Array.isArray(123);  // false (๋ฐฐ์—ด์ด ์•„๋‹ˆ๋ฏ€๋กœ)
    โœ‹ย console.table();์„ ํ™œ์šฉํ•˜๋ฉด console ์ฐฝ์—์„œ ๋ฐฐ์—ด์„ ๋ณด๊ธฐ ์ข‹๊ฒŒ ํ…Œ์ด๋ธ” ํ˜•ํƒœ๋กœ ์ถœ๋ ฅ ๊ฐ€๋Šฅ

โœ‹ย ๋ฐฐ์—ด ์›๋ณธ ๋ณ€๊ฒฝ O

  • push(): ๋ฐฐ์—ด ๋งจ ๋’ค์˜ element ์ถ”๊ฐ€ โ†’ ๋ฐฐ์—ด์˜ length ๋ฐ˜ํ™˜

  • pop(): ๋ฐฐ์—ด ๋งจ ๋’ค์˜ element ์‚ญ์ œ โ†’ ์ œ๊ฑฐํ•œ ๋งˆ์ง€๋ง‰ ์š”์†Œ ๋ฐ˜ํ™˜

  • unshift(): ๋ฐฐ์—ด ๋งจ ์•ž์˜ element ์ถ”๊ฐ€ โ†’ ๋ฐฐ์—ด์˜ length ๋ฐ˜ํ™˜

  • shift(): ๋ฐฐ์—ด ๋งจ ์•ž์˜ element ์‚ญ์ œ โ†’ ์ œ๊ฑฐํ•œ ์ฒซ๋ฒˆ์งธ ์š”์†Œ ๋ฐ˜ํ™˜

  • indexOf(): ํŠน์ • ๊ฐ’์˜ index ์ถœ๋ ฅ

    • ๋ฐฐ์—ด์— ์—†๋Š” ๊ฐ’์ผ ๊ฒฝ์šฐ -1 ์ถœ๋ ฅ
  • includes(): ํŠน์ • ๊ฐ’์ด ๋ฐฐ์—ด์— ์กด์žฌํ•˜๋Š”์ง€ ํŒ๋ณ„ (true/false)

    • Internet Explorer ๋ธŒ๋ผ์šฐ์ € ํ˜ธํ™˜ X
      // ๋ฐฐ์—ด์— ์ฐพ์œผ๋ ค๋Š” ๊ฐ’์ด ์žˆ๋Š”์ง€ ํŒ๋ณ„ํ•˜๋Š” ํ•จ์ˆ˜
      let words = ['Nice', 'to', 'meet', 'you'];
      
      function hasElement(arr, element) {
      	let isPresent = arr.indexOf(element) !== -1;  // element๊ฐ€ ๋ฐฐ์—ด์— ์žˆ์Œ์„ ์˜๋ฏธ
      	return isPresent;
      }
      
      hasElement(words, 'meet');  // true
      hasElement(words, 'me');    // false
      
      // includes()
      words.includes('Nice');  // true
      words.includes('why');   // false


โž•ย etc

โœ”๏ธย ์—ฐ์Šต๋ฌธ์ œ๋ฅผ ํ’€๊ณ  ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ ๋œ ๊ฐœ๋…๋“ค ์ •๋ฆฌ

  • slice(์‹œ์ž‘ํ•  ์š”์†Œ(ํฌํ•จO), ๋๋‚ผ ์š”์†Œ(ํฌํ•จX))
    • ๋ฐฐ์—ด ์›๋ณธ ๋ณ€๊ฒฝ X (immutable)
      // Q: ๋ฐฐ์—ด์„ ์ž…๋ ฅ๋ฐ›์•„ ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ ์š”์†Œ๊ฐ€ ์ œ์™ธ๋œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฆฌํ„ด
      
      function removeFromBackOfNew(arr) {
        let newArr = arr.slice();  // arr์™€ ๋˜‘๊ฐ™์€ ๋ฐฐ์—ด ์š”์†Œ๋“ค์„ ๋ณต์ œ (arr๋Š” ๋ณ€๊ฒฝ X)
        newArr.pop();
        return newArr;
      }
      
  • join(โ€™โ€™) : ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ์—ฐ๊ฒฐํ•˜์—ฌ ํ•˜๋‚˜์˜ ๋ฌธ์ž์—ด๋กœ ๋งŒ๋“ฌ
    const words = ['Hello', 'Hi'];
    
    words.join();    // "Hello,Hi"
    words.join('');  // "HelloHi"
    words.join('-'); // "Hello-Hi"
  • concat() : ์ธ์ž๋กœ ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์ด๋‚˜ ๊ฐ’๋“ค์„ ๊ธฐ์กด ๋ฐฐ์—ด์— ํ•ฉ์ณ์„œ ์ƒˆ ๋ฐฐ์—ด ๋ฐ˜ํ™˜
    arr1 = [1, 2];
    arr2 = [3, 4];
    num = 5;
    
    arr1.concat(arr2);  // [1, 2, 3, 4]
    arr1.concat(num);   // [1, 2, 5]
    
  • ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜ ๋ฌธ์ œ
    function fibonacci(num) {
      // ํ”ผ๋ณด๋‚˜์น˜ => ๋‹ค์Œ ์ˆ˜ = ๋งจ ๋์— ์žˆ๋Š” ์ˆ˜ + ๊ทธ ์•ž์— ์žˆ๋Š” ์ˆ˜
      // ์ˆซ์ž๋ฅผ ์ž…๋ ฅ๋ฐ›์•„์„œ num๋ฒˆ์งธ ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜์—ด์„ ๋ฆฌํ„ด
      // 1. ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜์—ด์„ ๋‹ด์•„์ค„ ๋ฐฐ์—ด์„ ํ•˜๋‚˜ ๋งŒ๋“ฌ
      // 2. ๋ฐ˜๋ณต๋ฌธ numํšŒ ๋ฐ˜๋ณตํ•˜๋ฉด์„œ
      // 3. ๋‘ ์ˆ˜์˜ ํ•ฉ์ด ๋‹ค์Œ ์ˆซ์ž -> ๋‘ ๊ฐœ์˜ ์ˆซ์ž๋Š” ๋ฐ˜๋“œ์‹œ ํ•„์š”
      // [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
    
      let fib = [];
      for (let i = 0; i <= num; i++) {
        if (i === 0 || i === 1) {
          fib.push(i); // [0, 1]
        } else {
          fib.push(fib[i-2] + fib[i-1]);
        }
      }
      return fib;
    }
profile
FE developer

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