โœ… ๋ฐฐ์—ด์— ์ถ”๊ฐ€ ๋˜๋Š” ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”์†Œ๋“œ

  • push : ๋งˆ์ง€๋ง‰ ์š”์†Œ ์ถ”๊ฐ€
  • pop : ๋งˆ์ง€๋ง‰ ์š”์†Œ ์‚ญ์ œ
  • shift : ์ฒซ ์š”์†Œ ์ถ”๊ฐ€
  • unshift : ์ฒซ ์š”์†Œ ์‚ญ์ œ
let numbers = [1, 2, 3, 4, 5, 6];
// ์›๋ณธ ๋ฐฐ์—ด์ด ๋ณ€ํ•ฉ๋‹ˆ๋‹ค!! ์ฃผ์˜ํ•˜์„ธ์š”!!
// ๋งˆ์ง€๋ง‰์— ์ถ”๊ฐ€ push
numbers.push(7);

console.log(numbers);
// ์ฒ˜์Œ์— ์ถ”๊ฐ€ push
numbers.unshift(0);

console.log(numbers);
// ๋งˆ์ง€๋ง‰ ์š”์†Œ ์‚ญ์ œ & ๋ฐ˜ํ™˜ pop
console.log(numbers.pop());

// ์ฒซ ์š”์†Œ ์‚ญ์ œ & ๋ฐ˜ํ™˜ shift
console.log(numbers.shift());

โœ… .splice()

  • ๋ฐฐ์—ด์˜ ๊ฐ’์„ ์‚ญ์ œ, ์ถ”๊ฐ€, ๊ต์ฒดํ•˜๋Š” ๋ฉ”์†Œ๋“œ
  • .splice(์‹œ์ž‘ํ• ์ธ๋ฑ์Šค, ์ œ๊ฑฐํ• ๊ฐœ์ˆ˜, ์ถ”๊ฐ€ํ•  ๋‚ด์šฉ)
const numbers1 = [1, 2, 3, 4];
const numbers2 = [1, 2, 3, 4];

console.log(numbers1.splice(0, 2));
console.log(numbers2.splice(2, 1, '77'));

// [3, 4];
// [1, 2, 77, 4];

โœ… ๋ฐฐ์—ด์˜ ๊ธธ์ด length()

  • ๋ฐฐ์—ด ๊ธธ์ด ๊ตฌํ•˜๋Š” ๋ฒ• : ๋ฐฐ์—ด.length
let numbers = [1, 2, 3, 4, 5, 6];
let fruits = ["์‚ฌ๊ณผ", "๋ฐ”๋‚˜๋‚˜", "์ˆ˜๋ฐ•", "ํฌ๋„", "ํŒŒ์ธ์• ํ”Œ"];

let numbersLength = numbers.length;
let fruitsLength = fruits.length;

console.log("numbers ๋ฐฐ์—ด์˜ ๊ธธ์ด๋Š”", numbersLength); //6
console.log("fruits ๋ฐฐ์—ด์˜ ๊ธธ์ด๋Š”", fruitsLength); //5

โœ… ๋ฐฐ์—ด ํ•ฉ์น˜๊ธฐ cancat()

  • ๋ฐฐ์—ด ํ•ฉ์น˜๋Š” ๋ฒ• : ๋ฐฐ์—ด.cancat
  • ์›๋ณธ์„ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š๊ณ  ํ•ฉ์ณ์ง€๋Š” ํŠน์ง•์ด ์žˆ์Œ
let numbers = [1, 2, 3, 4];
let fruits = ["์‚ฌ๊ณผ", "๋”ธ๊ธฐ", "์ˆ˜๋ฐ•"];

console.log(numbers.concat(fruits));
console.log(numbers);
console.log(fruits);

โœ… ๋ฐฐ์—ด, ๊ธฐ๋ณธ for๋ฌธ ์‚ฌ์šฉํ•˜๊ธฐ

let numbers = [1, 2, 3, 4, 5, 6];
let fruits = ["์‚ฌ๊ณผ", "๋ฐ”๋‚˜๋‚˜", "์ˆ˜๋ฐ•", "ํฌ๋„", "ํŒŒ์ธ์• ํ”Œ"];for (let i = 0; i < numbers.length; i++) {
  console.log(numbers[i]);
}
for (let i = 0; i < fruits.length; i++) {
  console.log(fruits[i]);
}

(์ฐธ๊ณ ๋กœ i๋Š” ํ•ด๋‹น ํ•จ์ˆ˜์—์„œ๋งŒ ์‚ฌ์šฉ๋˜๊ณ  ๋์ด๊ธฐ ๋•Œ๋ฌธ์— ํ•จ์ˆ˜๋งŒ ๋‹ค๋ฅด๋‹ค๋ฉด ๋™์ผํ•˜๊ฒŒ i์‚ฌ์šฉ ๊ฐ€๋Šฅ)

โœ… ๋ฐฐ์—ด, for of ์‚ฌ์šฉํ•˜๊ธฐ

let numbers = [1, 2, 3, 4, 5, 6];
let fruits = ["์‚ฌ๊ณผ", "๋ฐ”๋‚˜๋‚˜", "์ˆ˜๋ฐ•", "ํฌ๋„", "ํŒŒ์ธ์• ํ”Œ"];
let numbersLength = numbers.length;
let fruitsLength = fruits.length;
for (let number of numbers) {
  console.log(number);
}
for (let fruit of fruits) {
  console.log(fruit);
}

โœ… ๋ฐฐ์—ด, forEach ์‚ฌ์šฉํ•˜๊ธฐ

let numbers = [1, 2, 3, 4, 5, 6];
let fruits = ["์‚ฌ๊ณผ", "๋ฐ”๋‚˜๋‚˜", "์ˆ˜๋ฐ•", "ํฌ๋„", "ํŒŒ์ธ์• ํ”Œ"];
numbers.forEach(function (number, index, array) {
  console.log(number, index, array);
});
numbers.forEach((number, index, array) => {
  console.log(number, index, array);
});
fruits.forEach(function (fruit, i, arr) {
  console.log(fruit, i, arr);
});
fruits.forEach((fruit, i, arr) => {
  console.log(fruit, i, arr);
});

โœ… ๋ฐฐ์—ด, [].map

: ๋ฐฐ์—ดํ˜•ํƒœ๋กœ return๊ฐ’์„ ๋„ฃ์–ด์ฃผ๊ธฐ

let numbers = [1, 2, 3, 4, 5, 6];
let foreach = numbers.forEach(function (number) {
    return number;
});
let map = numbers.map(function (number) {
    return number;
});let map2 = numbers.map(number => {
    return number;
});
console.log("foreach :", foreach);
console.log("map :", map);
console.log("map2 :", map2);
const fruits = ["์‚ฌ๊ณผ", "ํŒŒ์ธ์• ํ”Œ", "์ˆ˜๋ฐ•", "ํฌ๋„", "์•„๋ฅ€์ง€"];
let obj = fruits.map((item, index) => {
    return {
        id: index,
        name: item,
    }
})
console.log(obj);

โœ… ๋ฐฐ์—ด, [].reduce

  • ๋ฐฐ์—ด๊ฐ’์˜ ํ•ฉ, item, index๊ฐ’, ๋ฐฐ์—ด์„ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค
  • "Return sum + item"์—์„œ sum์— ๊ฐ’์ด ์ด๋ฏธ ์žˆ์–ด์„œ item์„ ๋”ํ•ด์ฃผ๋ฉด ๋ฐฐ์—ด์— ๋”ํ•ด์ง€๋Š”๊ฒŒ์•„๋‹ˆ๋ผ sum์— ๋”ํ•ด์ ธ์„œ ๊ฐ’์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค
  • reduce๋Š” ๊ฐ๊ฐ์˜ return๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค
let numbers = [1, 2, 3, 4, 5, 6];
let reduce = numbers.reduce(function (sum, item, index, arr) {
    console.log(sum, item, index, arr);
    return sum + item; 
})

โ–ถ๏ธ์ž ๊น ํ’€์–ด๋ด๋„ ๋„์›€์ด ๋  ๊ฒƒ ๊ฐ™์•„์š”!!

๋ฌธ์ œ :
1) 1~100๊นŒ์ง€์˜ ๋ฐฐ์—ด์„ for๋ฌธ์„ ์‚ฌ์šฉํ•ด์„œ ๋งŒ๋“ค์–ด์ฃผ๊ณ 
2) 1) ๋ฐฐ์—ด์˜ ํ•ฉ์„ reduce๋ฅผ ์ด์šฉํ•ด์„œ ๊ตฌํ•ด๋ณด์ž๋ฉด?

๋ฌธ์ž์—ด๋„ ์‚ฌ์‹ค ๋ฐฐ์—ด

  • [...abc] => ...์€ ์ „๊ฐœ์—ฐ์‚ฐ์ž๋กœ abc๋ฅผ ๋ฐฐ์—ด๋กœ ๋ณด์—ฌ์ฃผ๋Š” ์šฉ๋„์ด๋‹ค
let str = "apple";

[...str].forEach(function (item, index, arr) {
   console.log(item, index, arr
   });
   
์ถœ๋ ฅ๋‚ด์šฉ -> ["a", "p", "p", "l", "e" ]

โœ… ๋ฐฐ์—ด ํ•จ์ˆ˜, filter

  • ์กฐ๊ฑด์— ๋ถ€ํ•ฉํ•˜๋Š” ๋ฐฐ์—ด ์š”์†Œ๋งŒ ๋ฐ˜ํ™˜
  • [].filter((๋งค๊ฐœ๋ณ€์ˆ˜) => ์กฐ๊ฑด)
  • ๋‹จ, ์กฐ๊ฑด์ด true์ผ๋•Œ๋งŒ ์ž‘๋™ํ•œ๋‹ค
let numbers = [1, 2, 3, 4, 5, 6];
let arr;

 arr = numbers.filter((num) => num > 3);
 console.log(arr);
 
 const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
 
 const result = words.filter(word => word.length > 6);
 console.log(result);
  • ๋ณดํ†ต ์ด๋ ‡๊ฒŒ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋Š”๋ฐ ์•„๋ž˜์ฒ˜๋Ÿผ ๋ณดํ†ต ํ™”์‚ดํ‘œํ•จ์ˆ˜๋กœ ์ค„์—ฌ์„œ ์‚ฌ์šฉํ•œ๋‹ค

<์˜ˆ์‹œ1>

ํ’€์–ด์„œ์ž‘์„ฑํ•œ ํ•จ์ˆ˜
let arr1 = numbers.filter(function(item){
  return item> 3;
})

ํ™”์‚ดํ‘œํ•จ์ˆ˜
let arr2 = numbers.filter((item) => item> 3);

<์˜ˆ์‹œ 2>

ํ’€์–ด์„œ์ž‘์„ฑํ•œ ํ•จ์ˆ˜
let arr3 = words.filter(function(item) {
  return item,length >= 6;
})

ํ™”์‚ดํ‘œํ•จ์ˆ˜
 const result = words.filter(word => word.length > 6);

โœ… ๋ฐฐ์—ด ํ•จ์ˆ˜, includes

  • filter์™€ ๋น„์Šทํ•˜์ง€๋งŒ includes๋Š” ์ง€์ •ํ•œ ์š”์†Œ๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๋ฉ”์†Œ๋“œ
  • ํ•จ์ˆ˜์ด๋ฆ„.includes("ํฌํ•จํ•˜๊ณ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ณ  ์‹ถ์€ text");
let numbers = [1, 2, 3, 4, 5, 6];
console.log(numbers.includes(3));
console.log(numbers.includes(7));
const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
console.log(words.includes('elite'));
console.log(words.includes('pororo'));

โ–ถ๏ธ์ž ๊น ํ’€์–ด๋ด๋„ ๋„์›€์ด ๋  ๊ฒƒ ๊ฐ™์•„์š”!!

๋ฌธ์ œ :

1) ์œ„์˜ ๋‘ ๋ฐฐ์—ด์—์„œ ๋™์ผํ•œ ์š”์†Œ๋งŒ์„ ๊ฐ€์ง€๋Š” ๋ฐฐ์—ด same ๋งŒ๋“ค๊ธฐ
2) ์œ„์˜ ๋‘ ๋ฐฐ์—ด์—์„œ ์„œ๋กœ ๋‹ค๋ฅธ ์š”์†Œ๋งŒ์„ ๊ฐ€์ง€๋Š” ๋ฐฐ์—ด diff ๋งŒ๋“ค๊ธฐ

โœ… ๋ฐฐ์—ด ํ•จ์ˆ˜, find / findindex

const fruits = ["Apple", "Banana", "Cherry"];
const result1 = fruits.find(item => {
    return /^A/.test(item);
})
const result2 = fruits.findIndex(item => {
    return /^C/.test(item);
})
console.log(result1);
console.log(result2);
profile
ษช สœแดแด˜แด‡ แด›แด sแดสŸแด แด‡ ส€แด‡แด€สŸ แด˜ส€แดส™สŸแด‡แดs๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ปโค๏ธ

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