[4] Array

Naakiteยท2022๋…„ 2์›” 13์ผ
0

๐Ÿ“๋“œ๋ฆผ์ฝ”๋”ฉ JavaScript

๋ชฉ๋ก ๋ณด๊ธฐ
4/6
post-thumbnail

โœ… ๋ฐฐ์—ด ์„ ์–ธ

const arr1 = new Array();
const arr2 = [1,2];
// ๋‘๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.

โœ… ๋ฐฐ์—ด ์•ˆ์— ์žˆ๋Š” ์ „์ฒด ์•„์ดํ…œ ์ถœ๋ ฅ

const fruits = ['๐Ÿ’','๐ŸŠ'];
//a. for
for (let i = 0; i<fruits.length; i++){
    console.log(fruits);
}

//b. for of 
for (let fruit of fruits){
    console.log(fruit);
}

//c. forEach
fruits.forEach((fruit) => consol.log(fruit));

โœ… push(), pop(), unshift(), shift()

// Addtion, deletion, copy
// push: ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค์— ์•„์ดํ…œ ์ถ”๊ฐ€
fruits.push('๐Ÿ“');

//pop: ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ ์•„์ดํ…œ๋ฅผ ์ œ๊ฑฐ
fruits.pop();

// unshift: ๋ฐฐ์—ด์˜ ์ฒซ๋ฒˆ์งธ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ ์•„์ดํ…œ ์ถ”๊ฐ€
fruits.unshift('๐Ÿ‹');

// shift: ๋ฐฐ์—ด์˜ ์ฒซ๋ฒˆ์งธ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ ์•„์ดํ…œ ์‚ญ์ œ
fruits.shift();

unshift : ์›๋ž˜ ์žˆ๋˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋’ท ๋ฐ•์Šค๋“ค์— ์ฐจ๋ก€๋Œ€๋กœ ์˜ฎ๊ฒจ ๋‹ด์•„, ์ธ๋ฑ์Šค ๋งจ ์•ž์— ๊ณต๊ฐ„์„ ๋งŒ๋“ค์–ด์„œ ์•„์ดํ…œ์„ ์ถ”๊ฐ€ํ•จ.
shift : ๋งจ ์•ž์— ์žˆ๋˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ง€์šฐ๊ณ  ์›๋ž˜์žˆ๋˜ ๋ฐ์ดํ„ฐ๋“ค์„ ํ•˜๋‚˜์”ฉ ์•ž์œผ๋กœ ๋ฐ€์–ด์ค˜์•ผํ•จ.
=> ์ด๋Ÿฐ ์ด์œ ๋กœ pop, push ๋ณด๋‹ค ํ›จ์”ฌ ๋Š๋ฆฌ๋‹ค!

๐Ÿ“ splice๋ฅผ ์‚ฌ์šฉํ•˜์ž! (์ง€์ •ํ•œ ์œ„์น˜์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œ,์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.)

fruits.splice(1,1) // ์ธ๋ฑ์Šค 1๋ถ€ํ„ฐ 1๊ฐœ ์‚ญ์ œ.
// ๋งŒ์•ฝ ๊ฐฏ์ˆ˜๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด..?
fruits.splice(2); // ์ธ๋ฑ์Šค 2๋ถ€ํ„ฐ ๋ฐฐ์—ด๋๊นŒ์ง€ ์‚ญ์ œ.
fruits.splice(1,1,'๐ŸŠ','๐Ÿ“'); // ์ธ๋ฑ์Šค 1๋ฒˆ๋ถ€ํ„ฐ 1๊ฐœ๋ฅผ ์ง€์šฐ๊ณ  ์•„์ดํ…œ ๋‘๊ฐœ ์ถ”๊ฐ€

โœ… cocat(), indexOf(), lastindexOf(), includes()

// combine two arrays 
const fruits2 = ['๐Ÿ’','๐ŸŠ'];
const newFruits = fruits.concat(fruits2);

//Searching, find the index
console.log(fruits.indexOf('๐ŸŠ')); // ์•„์ดํ…œ์ด ์œ„์น˜ํ•œ ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ๋ฅผ ์ถœ๋ ฅ , ๋งŒ์•ฝ ์—†๋‹ค๋ฉด -1 ์ถœ๋ ฅ
// ๋งŒ์•ฝ ์˜ค๋ Œ์ง€๊ฐ€ ๋‘๊ฐœ๋ผ๋ฉด indexOf ๋Š” ์•ž์— ์žˆ๋Š” ์ธ๋ฑ์Šค๋ฅผ ์ถœ๋ ฅํ•ด์ค€๋‹ค. 
// ๋งŒ์•ฝ ๋งˆ์ง€๋ง‰์— ์œ„์น˜ํ•œ ์ธ๋ฑ์Šค๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด
// lastindexOf ์„ ์‚ฌ์šฉํ•˜๋ฉด๋œ๋‹ค!
console.log(fruits.lastindexOf('๐ŸŠ')); 
console.log(fruits.includes('๐ŸŠ'));  // ์•„์ดํ…œ์ด ๋ฐฐ์—ด ์›์†Œ๋กœ ์žˆ๋Š”์ง€ true or false ์ถœ๋ ฅ

๐ŸŽฏ ์ค‘์š”ํ•œ ํ•จ์ˆ˜๋“ค!

1. join()

๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ๋ฌธ์ž์—ด๋กœ ํ•˜๋‚˜์˜ ์›์†Œ๋กœ ๋งŒ๋“œ๋Š” ํ•จ์ˆ˜๋‹ค.(์‰ฝ๊ฒŒ ๋งํ•ด, ๋ฐฐ์—ด์˜ ํ•ญ๋ชฉ์„ ํ•˜๋‚˜์˜ ๋ฌธ์ž๋กœ ํ•ฉ์นœ๋‹ค.) ๊ฐ ์š”์†Œ๋“ค์„ , ์œผ๋กœ ๊ตฌ๋ถ„ํ•˜๋Š”๋ฐ, ๋‹ค๋ฅธ ๋ฌธ์ž๋กœ ๊ตฌ๋ถ„ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด, ( ) ์•ˆ์— ์‚ฌ์šฉ์ž๊ฐ€ ์ž„์˜๋กœ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

const elements = ['red', 'yellow', 'green'];

console.log(elements.join());
//"red,yellow,green"

console.log(elements.join(''));
//"redyellogreen"

console.log(elements.join('-'));
//"red-yellow-green"

2. split(separator, limit)

๋ฌธ์ž์—ด์„ ์ผ์ •ํ•œ ๊ตฌ๋ถ„์ž๋กœ ์ž˜๋ผ์„œ, ๋ฐฐ์—ด๋กœ ๋งŒ๋“œ๋Š” ํ•จ์ˆ˜๋‹ค. ์ด๋•Œ ์‚ฌ์šฉ์ž๊ฐ€ limit์„ ์ง€์ •ํ•˜๋ฉด, ์‚ฌ์šฉ์ž๊ฐ€ ์ง€์ •ํ•œ limit๊นŒ์ง€ ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

const fruits = '๐ŸŽ, ๐Ÿฅ, ๐ŸŒ, ๐Ÿ’';
const result = fruits.split(',');
console.log(result); //๊ฒฐ๊ณผ: ["๐ŸŽ", " ๐Ÿฅ", " ๐ŸŒ", " ๐Ÿ’"]
const result1 = fruits.split(',',2); // ๊ทธ๋Ÿผ ํ‚ค์œ„๊นŒ์ง€ ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค์–ด์ง
console.log(result1); //๊ฒฐ๊ณผ: ["๐ŸŽ", " ๐Ÿฅ"]

3. reverse()

๋ฐฐ์—ด ์ˆœ์„œ๊ฐ€ ๋ฐ˜์ „๋˜๊ฒŒ ๋ณ€ํ™˜ํ•œ๋‹ค.

 {
    const array = [1, 2, 3, 4, 5];
    const result = array.reverse(); // ๋ฐฐ์—ด ์ž์ฒด๋ฅผ reverse
  } // [5,4,3,2,1]

4. splice(start[, deleteCount[, item1[, item2[, ...]]]])

์›๋ณธ ๋ฐฐ์—ด์— ์ƒˆ๋กœ์šด ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜, ๊ธฐ์กด ์š”์†Œ๋ฅผ ์‚ญ์ œ, ๊ต์ฒดํ•˜์—ฌ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

  • start : ๋ฐฐ์—ด์˜ ๋ณ€๊ฒฝ์„ ์‹œ์ž‘ํ•  ์ธ๋ฑ์Šค.
  • deleteCount: ๋ฐฐ์—ด์—์„œ ์ œ๊ฑฐํ•  ์š”์†Œ์˜ ์ˆ˜
  • item1, item2... : ๋ฐฐ์—ด์— ์ถ”๊ฐ€ํ•  ์š”์†Œ.
 { 
    const array = [1, 2, 3, 4, 5];
    const result = array.splice(0,2); // ์ธ๋ฑ์Šค 0-1 ๊นŒ์ง€ ๋ฐฐ์—ด ์ œ๊ฑฐ
    console.log(result); // [1,2] 
    console.log(array); // [3,4,5]
 }
 {
    const array = [1, 2, 3, 4, 5];
    const result = array.splice(0,2,8); // ์ธ๋ฑ์Šค 0-1๊นŒ์ง€ ๋ฐฐ์—ด ์ œ๊ฑฐ ํ›„, ์ˆซ์ž 8 ์ถ”๊ฐ€
    console.log(result); // [1,2]
    console.log(array); // [8,3,4,5]
 }

5. slice([begin[, end]])

๋ฐฐ์—ด์„ ์ง€์ •ํ•œ ๋ฒ”์œ„๋งŒํผ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด๋กœ ๋งŒ๋“ ๋‹ค.
(์ด๋•Œ, ์‹œ์ž‘์ธ๋ฑ์Šค์™€ ์ข…๋ฃŒ์ธ๋ฑ์Šค๋ฅผ ์ง€์ •ํ•˜๋Š”๋ฐ, ์ด๋•Œ ์ข…๋ฃŒ์ธ๋ฑ์Šค๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด, ๋ฐฐ์—ด์˜ ๋๊นŒ์ง€ ์ž˜๋ผ๋‚ธ๋‹ค.)

{
  const array = [1, 2, 3, 4, 5];
  const result = array.slice(0,2); // ์ธ๋ฑ์Šค 0-1๊นŒ์ง€ ๋ฐฐ์—ด ์ œ๊ฑฐ ํ›„, ์ˆซ์ž 8 ์ถ”๊ฐ€
  console.log(result); // [1,2]
  console.log(array); // [1,2,3,4,5] // 
}
{
  const array = [1, 2, 3, 4, 5];
  const result = array.slice(0); // ์ธ๋ฑ์Šค 0-1๊นŒ์ง€ ๋ฐฐ์—ด ์ œ๊ฑฐ ํ›„, ์ˆซ์ž 8 ์ถ”๊ฐ€
  console.log(result); // [1,2,3,4,5]
  console.log(array); // [1,2 ,3,4,5]
}

๐ŸŽ splice ์™€ slice ์ฐจ์ด์ 

splice๋Š” ์›๋ณธ ๋ฐฐ์—ด์„ ์ˆ˜์ •ํ•˜์ง€๋งŒ, slice๋Š” ๊ธฐ์กด ๋ฐฐ์—ด์„ ์ˆ˜์ •ํ•˜์ง€ ์•Š๋Š”๋‹ค.

6. find(callback, [thisArg])

๋ฐฐ์—ด์—์„œ ํŠน์ • ๊ฐ’์„ ์ฐพ๋Š” ์กฐ๊ฑด์„ callback ํ•จ์ˆ˜๋กœ ์ „๋‹ฌํ•˜์—ฌ, ์ฃผ์–ด์ง„ ์กฐ๊ฑด์˜ ์ฒซ๋ฒˆ์งธ ์š”์†Œ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. (๋งŒ์•ฝ ์กฐ๊ฑด์— ๋งž๋Š” ์š”์†Œ ๊ฐ’์ด ์—†์„ ๊ฒฝ์šฐ undefined ๋ฆฌํ„ด.

  • callback: ๋ฐฐ์—ด์˜ ๊ฐ ๊ฐ’์— ๋Œ€ํ•ด ์‹คํ–‰ํ•  ํ•จ์ˆ˜.
  • thisArg: ์ฝœ๋ฐฑ์ด ํ˜ธ์ถœ๋  ๋•Œ this ๋กœ ์‚ฌ์šฉํ•  ๊ฐ์ฒด.
{
  const array = [1,2,3,4,5];
  const found = array.find(element => element>3);
  console.log(found); // 4
}

๋ฐฐ์—ด์—์„œ ์กฐ๊ฑด์— ๋งž๋Š” ๊ฐ์ฒด๋ฅผ ์ฐพ์„ ์ˆ˜๋„ ์žˆ๋‹ค!

class Student {
    constructor(name, age, enrolled, score) {
      this.name = name;
      this.age = age;
      this.enrolled = enrolled;
      this.score = score;
    }
  }
  const students = [
    new Student('A', 29, true, 45),
    new Student('B', 28, false, 80),
    new Student('C', 30, true, 90),
    new Student('D', 40, false, 66),
    new Student('E', 18, true, 88),
  ];
  
  // Q5. find a student with the score 90
  {
    const result = students.find((student) =>{
        return student.score > 70; 
    });
    console.log(result);
    
  }

7. filter (callback(element[, index[, array]])[, thisArg])

find ํ•จ์ˆ˜์ฒ˜๋Ÿผ ์กฐ๊ฑด์— ๋งž๋Š” ๊ฐ’์„ ์ฐพ์ง€๋งŒ, ์ฒซ๋ฒˆ์งธ ์š”์†Œ๋งŒ ๋ฐ˜ํ™˜ํ•˜๋Š” findํ•จ์ˆ˜์™€๋Š” ๋‹ฌ๋ฆฌ ๋ชจ๋“  ๊ฐ’์„ ๋ฐฐ์—ด ํ˜•ํƒœ๋กœ ๋ฆฌํ„ดํ•œ๋‹ค.

// result should be: [45, 80, 90, 66, 88]
 {
    const result = students.map((student) => student.score);
 }

8. map(callback(currentValue[, index[, array]])[, thisArg])

๋ฐฐ์—ด ๋‚ด์˜ ๋ชจ๋“  ์š”์†Œ ๊ฐ๊ฐ์— ๋Œ€ํ•˜์—ฌ ์ฃผ์–ด์ง„ ์กฐ๊ฑด์— ๋งž๋Š” ์š”์†Œ ๊ฐ’๋“ค์„ ๋ชจ์•„ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

  • callback : ์ƒˆ๋กœ์šด ๋ฐฐ์—ด ์š”์†Œ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ํ•จ์ˆ˜.
    • currentValue: ์ฒ˜๋ฆฌํ•  ํ˜„์žฌ ์š”์†Œ
    • index: ์ฒ˜๋ฆฌํ•  ํ˜„์žฌ ์š”์†Œ์˜ ์ธ๋ฑ์Šค
    • array: map( ) ์„ ํ˜ธ์ถœํ•œ ๋ฐฐ์—ด
  • thisArg: callback์„ ์‹คํ–‰ํ•  ๋•Œ this ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๊ฐ’.
  {
     const result = students.map((student) => student.score); 
    //[45, 80, 90, 66, 88]
  }

9. reduce(callback[, initialVlue])

๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฃผ์–ด์ง„ ๋ฆฌ๋“€์„œ ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๊ณ , ํ•˜๋‚˜์˜ ๊ฒฐ๊ณผ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
๋ฆฌ๋“€์„œ ํ•จ์ˆ˜์—๋Š” ๋ˆ„์‚ฐ๊ธฐ, ํ˜„์žฌ ๊ฐ’, ํ˜„์žฌ ์ธ๋ฑ์Šค, ์›๋ณธ ๋ฐฐ์—ด ์ด ๋„ค๊ฐœ์˜ ์ธ์ž๋ฅผ ๊ฐ€์ง„๋‹ค.

  {
    const result = students.reduce((prev, current)=> prev + current.score ,0); // ํ•™์ƒ๋“ค์˜ ์ ์ˆ˜์˜ ํ•ฉ์ด ์ถœ๋ ฅ๋œ๋‹ค.
  } 
  
Q) ์ ์ˆ˜๊ฐ€ 50์  ์ด์ƒ์ธ ํ•™์ƒ๋“ค๋งŒ ์ถœ๋ ฅํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด..?
class Student {
    constructor(name, age, enrolled, score) {
      this.name = name;
      this.age = age;
      this.enrolled = enrolled;
      this.score = score;
    }
  }
  const students = [
    new Student('A', 29, true, 45),
    new Student('B', 28, false, 80),
    new Student('C', 30, true, 90),
    new Student('D', 40, false, 66),
    new Student('E', 18, true, 88),
  ];

์ •๋‹ต

   const result = students
    .map((student) => student.score)
    .filter((score) => score >= 50)
    .join();
  
 

๋“œ๋ฆผ์ฝ”๋”ฉ ๊ฐ•์˜๋ฅผ ๋“ฃ๊ณ  ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค.

profile
๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿƒโ€โ™€๏ธ

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