๐ŸŒŸย 220712.TIL

Universeยท2022๋…„ 7์›” 12์ผ
0

study

๋ชฉ๋ก ๋ณด๊ธฐ
15/49
post-custom-banner

๋ฐฐ์—ด ์ž๋ฅด๊ณ  ํ† ๋ง‰๋‚ด๊ธฐ


1. pop

   let fruits = ['apple', 'banana', 'grapes', 'mango', 'orange'];
   fruits.pop();
   
   fruits = ['apple', 'banana', 'grapes', 'mango'];

๋ฐฐ์—ด ๋งจ ๋’ค์˜ ๊ฐ’์„ ์‚ญ์ œ

2. push

    let fruits = ['apple', 'banana', 'grapes', 'mango', 'orange'];
    fruits.push('melon');
    
    fruits = ['apple', 'banana', 'grapes', 'mango', 'orange', 'melon'];

๋ฐฐ์—ด ๋งจ ๋’ค์— ๊ฐ’์„ ์‚ฝ์ž…

3. shift

    let fruits = ['apple', 'banana', 'grapes', 'mango', 'orange'];
    fruits.shift();
    
    fruits = ['banana', 'grapes', 'mango', 'orange'];

๋ฐฐ์—ด ๋งจ ์•ž์˜ ๊ฐ’์„ ์‚ญ์ œ

4. unshift

let fruits = ['apple', 'banana', 'grapes', 'mango', 'orange'];
fruits.unshift('melon');

fruits = ['melon', 'apple', 'banana', 'grapes', 'mango'];

๋ฐฐ์—ด ๋งจ ์•ž์— ๊ฐ’์„ ์‚ฝ์ž…

5. splice

let fruits = ['apple', 'banana', 'grapes', 'mango', 'orange'];
fruits.splice(1, 1, 'melon'); // 1๋ฒˆ์งธ ์ธ๋ฑ์Šค์—์„œ 1๊ฐœ๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  'melon'์„ ์ถ”๊ฐ€

fruits = ['apple', 'melon', 'grapes', 'mango', 'orange']

index(์ฒซ๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ)๋ถ€ํ„ฐ ์ œ๊ฑฐํ•  ์š”์†Œ ๊ฐฏ์ˆ˜(๋‘๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ) ๊นŒ์ง€ ์‚ญ์ œํ•˜๊ณ 
์ถ”๊ฐ€์š”์†Œ(์„ธ๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ)๋ฅผ ์‚ฝ์ž…
์ฃผ์˜ํ•  ์ ์€ ์ฒซ๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ๋ฐฐ์—ด์˜ ์œ„์น˜์ด์ง€๋งŒ ๋‘๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ์š”์†Œ ๊ฐœ์ˆ˜

0713 ์ถ”๊ฐ€

//๋ณ€์ˆ˜์— ํ• ๋‹นํ•  ๋•Œ
let array1 = fruits.splice(3);
array1 = ['mango', 'orange']
fruits = ['apple', 'banana', 'grapes']
// ์ž๋ฅธ ๊ฐ’์ด ์ €์žฅ๋œ๋‹ค
// ๋˜ํ•œ ์Œ์ˆ˜๋ฅผ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ง€์ •ํ•˜๋ฉด
let array2 = fruits.splice(-1,1)
array2 = ['orange']
fruits = ['apple', 'banana', 'grapes', 'mango']
// ๋’ค์—์„œ ๋ถ€ํ„ฐ ์ €์žฅ์ด ๋œ๋‹ค

6. slice

    let fruits = ['apple', 'banana', 'grapes', 'mango', 'orange'];
    let fruitsArray = fruits.slice(1, 2);
    
    fruitsArray = ['banana']

startindex(์ฒซ๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ) ๋ถ€ํ„ฐ endindex(๋‘๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ) ๊นŒ์ง€
๊ธฐ์กด ๋ฐฐ์—ด์— ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š๋Š” ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ์ƒ์„ฑ
์ฃผ์˜ํ•  ์ ์€ ๋‘๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ ์ง์ „๊นŒ์ง€ ํ• ๋‹นํ•˜๊ธฐ ๋•Œ๋ฌธ์—
์œ„์— ์ฝ”๋“œ์ฒ˜๋Ÿผ slice(1,2)๋ฅผ ํ–ˆ์ง€๋งŒ โ€˜bananaโ€™ ๋งŒ ์ถœ๋ ฅ๋˜๋Š” ๋ชจ์Šต

7. reverse

let fruits = ['apple', 'banana', 'grapes', 'mango', 'orange'];
fruits.reverse();
= (5) ['orange', 'mango', 'grapes', 'banana', 'apple']

ํ•จ์ˆ˜์˜ ์ˆœ์„œ๋ฅผ ๊ฑฐ๊พธ๋กœ ์›๋ณธ ๋ฐฐ์—ด์„ ๋ณ€๊ฒฝํ•œ๋‹ค

8. concat

    let array1 = ['a', 'b', 'c'];
    let array2 = ['d', 'f', 'g'];
    let array3 = array1.concat(array2);
    array3 = ['a', 'b', 'c', 'd', 'f', 'g']

๊ธฐ์ค€ ๋ฐฐ์—ด์— ๋Œ€์ƒ ๋ฐฐ์—ด์„ ๋ณ‘ํ•ฉํ•˜์—ฌ ๊ธฐ์กด ๋ฐฐ์—ด์— ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š๋Š” ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ์ƒ์„ฑ

9. every & some

    let array = [1,2,3,4,5,6,7,8,9];
    let even = e => e % 2 === 0;
    array.every(even) = false;
    array.some(even) = true;

๋‘˜ ๋‹ค boolean ๊ฐ’์„ ๋ฐ˜ํ™˜
every ๋Š” ๋ฐฐ์—ด์ด ํ•จ์ˆ˜์˜ ์กฐ๊ฑด ์ค‘ false ๊ฐ€ ๋‹จ ํ•˜๋‚˜๋ผ๋„ ์žˆ๋‹ค๋ฉด false๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ์ „๋ถ€ true ๋ฉด true
some ์€ ๋ฐฐ์—ด์ด ํ•จ์ˆ˜์˜ ์กฐ๊ฑด ์ค‘ true ๊ฐ€ ๋‹จ ํ•˜๋‚˜๋ผ๋„ ์žˆ๋‹ค๋ฉด true๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ์ „๋ถ€ false ๋ฉด false

10. filter

    // filter ๋กœ ๋ฐฐ์—ด์—์„œ ์ง์ˆ˜๋งŒ ๊ณจ๋ผ๋‚ด๋ณด์ž
    // ๋ฐฉ๋ฒ• 1
    let array = [1,2,3,4,5,6,7,8,9];
    let filterArray = array.filter(e => e % 2 === 0);
    filterArray = [2, 4, 6, 8]
    
    // ๋ฐฉ๋ฒ• 2
    let array = [1,2,3,4,5,6,7,8,9];
    function even(e) {
        return e % 2 === 0;
    }
    let filterArray = array.filter(even);
    filterArray = [2, 4, 6, 8]
    
    // ๋ฐฐ์—ด์—์„œ ์–ด๋–ค ํŠน์ • ๊ฐ’์„ ๊ฐ€์ง„ ๋ฐฐ์—ด๋งŒ ๊ณจ๋ผ๋‚ด๋ณด์ž
    // ์˜ฌํ•ด ์„œ๋ฅธ์ธ ์‚ฌ๋žŒ์„ ๊ณจ๋ผ๋ณผ๊นŒ ?
    let array = [
      { name: 'merry', age: '30'},
      { name: 'jane', age: '29'},
      { name: 'joon', age: '30'},
      { name: 'eli', age: '27'},
      { name: 'bob', age: '25'}
    ];
    let thirtyYearsOld = array.filter(e => e.age === '30');
    
    thirtyYearsOld
    (2) [{โ€ฆ}, {โ€ฆ}]
    	0: {name: 'merry', age: '30'}
    	1: {name: 'joon', age: '30'}
    	length: 2
    	[[Prototype]]: Array(0)

๋ฐฐ์—ด์„ ์ˆœ์„œ๋Œ€๋กœ ๋ถˆ๋Ÿฌ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด callback ํ•จ์ˆ˜์„ ์‹คํ–‰ํ•˜๊ณ 
๊ฒฐ๊ณผ๊ฐ€ true์ธ ์š”์†Œ๋“ค๋งŒ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜

11. forEach & map

    // ๊ธฐ์กด ๋ฐฐ์—ด์— 5์”ฉ ๋”ํ•˜๊ธฐ
    let array = [1, 2, 3, 4, 5];
    
    array.forEach( (m,n) => {
      array[n] = m + 5
    });
    
    array
    (5) [6, 7, 8, 9, 10] // ์‚ฌ์šฉ์— ์ฃผ์˜ํ•  ๊ฒƒ
    
    // ๊ธฐ์กด ๋ฐฐ์—ด์— 5์”ฉ ๋”ํ•ด์„œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋งŒ๋“ค๊ธฐ
    let array = [1, 2, 3, 4, 5];
    
    let mapArray = array.map( e => {
      return e + 5;
    });
    
    mapArray
    (5) [6, 7, 8, 9, 10]

forEach ์™€ map ๋„ filter ์ฒ˜๋Ÿผ ๋ฐฐ์—ด์„ ์ˆœ์„œ๋Œ€๋กœ ๋ถˆ๋Ÿฌ callback ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์€ ๊ฐ™๋‹ค
์ฐจ์ด์ ์€ ์ˆ˜์ •๋œ ๋ฐฐ์—ด์„ ๋ฆฌํ„ด(Return)ํ•˜๋Š”์ง€ ์—ฌ๋ถ€
callback ํ•จ์ˆ˜ ์‹คํ–‰ ํ›„ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ return ๋ฐ›๊ณ ์‹ถ๋‹ค๋ฉด map
๊ธฐ์กด๋ฐฐ์—ด์„ ๊ฐ€๊ณตํ•˜์—ฌ ํ‰๊ท ์ด๋‚˜ ํ•ฉ์‚ฐ์„ ๊ตฌํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด forEach

์กฐ๊ธˆ ์ดํ•ดํ•˜๊ธฐ ํž˜๋“ค์—ˆ๋˜ ๊ฐœ๋…

forEach ๋ฅผ ์‚ฌ์šฉํ• ๋•Œ๋Š” ์–‡์€ ๋ณต์‚ฌ ๊ฐ€ ์ˆ˜ํ–‰๋˜์–ด ๊ธฐ์กด์˜ ๋ฐฐ์—ด์ด ์ˆ˜์ •๋˜๋ฏ€๋กœ ์ฃผ์˜ํ•ด์•ผ ํ•œ๋‹ค
์–‡์€ ๋ณต์‚ฌ๋Š” ์‹ค์ œ ๊ฐ’์„ ๋ณต์‚ฌํ•œ๊ฒŒ ์•„๋‹Œ ๋ฉ”๋ชจ๋ฆฌ์˜ ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ•œ ๊ฒƒ์œผ๋กœ ์ฐธ์กฐ๋œ ์‹ค์ œ ๊ฐ’์€ ๊ฐ™๋‹ค

...
๐Ÿง๐Ÿง๐Ÿง๐Ÿง๐Ÿง๐Ÿง๐Ÿง๐Ÿง๐Ÿง๐Ÿง


ํ‰๊ฐ€

๋ฐฐ์—ด์„ ์•„์˜ˆ ๋ชจ๋ฅด๋Š”๊ฑด ์•„๋‹ˆ์—ˆ๋Š”๋ฐ
๋ชจ๋ฅด๋Š”๊ฒŒ ์•„๋‹Œ๊ฒŒ ์•„๋‹ˆ์—ˆ๋˜ ๊ฒƒ ๊ฐ™๋‹ค ๐Ÿ˜‡

profile
Always, we are friend ๐Ÿงก
post-custom-banner

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