language/javascript

๋ฐฐ์—ด ๊ฐœ๋…๊ณผ APIs

๊ฑด๋ง๋”” 2021. 11. 25. 22:51
Object
- ์„œ๋กœ ์—ฐ๊ด€๋œ ํŠน์ง•๊ณผ ํ–‰๋™๋“ค์„ ๋ฌถ์–ด๋†“๋Š” ๊ฒƒ

์ž๋ฃŒ๊ตฌ์กฐ
- ๋น„์Šทํ•œ type์˜ object๋“ค์„ ๋ฌถ์–ด๋†“๋Š” ๊ฒƒ(dynamically typed language)
- ๋‹ค๋ฅธ ์–ธ์–ด์˜ ๊ฒฝ์šฐ์—๋Š” ๋™์ผํ•œ type์˜ object๋งŒ์„ ๋ฌถ์„ ์ˆ˜ ์žˆ๋‹ค.

 

1. Declaration

const arr1 = new Array();
const arr2 = [1, 2];

 

2. Index position

const fruits = ['๐ŸŽ', '๐ŸŒ'];
console.log(fruits); // ['๐ŸŽ', '๐ŸŒ']
console.log(fruits.length); // 2
console.log(fruits[0]); // ๐ŸŽ
console.log(fruits[1]); // ๐ŸŒ
console.log(fruits[2]); // undefined
console.log(fruits[fruits.length - 1]); // ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ item ์ ‘๊ทผ, ๐ŸŒ

 

3. Looping over an array

// print all fruits
// a. for
for (let i = 0; i < fruits.length; i++) {
	console.log(fruits[i]);
}

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

// c. forEach
// ๋ฐฐ์—ด ์•ˆ์˜ value๋“ค ๋งˆ๋‹ค ์ „๋‹ฌํ•œ ํ•จ์ˆ˜๋ฅผ ์ถœ๋ ฅ
fruits.forEach((fruit) => console.log(fruit));

 

4. Addition, deletion, copy

// push: add an item to the end
fruits.push('๐Ÿ“', '๐Ÿ‘');
console.log(fruits); // ['๐ŸŽ', '๐ŸŒ', '๐Ÿ“', '๐Ÿ‘']

// pop: remove an item from the end
fruits.pop(); // ['๐ŸŽ', '๐ŸŒ', '๐Ÿ“']
fruits.pop(); // ['๐ŸŽ', '๐ŸŒ']
console.log(fruits);

// unshift: add an item to the beginning
fruits.unshift('๐Ÿ“','๐Ÿ‹');
console.log(fruits); // ['๐Ÿ“', '๐Ÿ‹', '๐ŸŽ', '๐ŸŒ']

// shift: remove an item from the beginning
fruits.shift(); // ['๐Ÿ‹', '๐ŸŽ', '๐ŸŒ']
fruits.shift(); // ['๐ŸŽ', '๐ŸŒ']
console.log(fruits);

// splice: remove an item by index position
fruits.push('๐Ÿ“', '๐Ÿ‘', '๐Ÿ‹');
console.log(fruits); // ['๐ŸŽ', '๐ŸŒ', '๐Ÿ“', '๐Ÿ‘', '๐Ÿ‹']
// splice(์‹œ์ž‘ index, ์ง€์šธ item ๊ฐœ์ˆ˜(option))
fruits.splice(1);
console.log(fruits); // ['๐ŸŽ'], ์ง€์ •ํ•œ index๋ถ€ํ„ฐ ๋ชจ๋“  item ์‚ญ์ œ
fruits.splice(1, 1);
console.log(fruits); // ['๐ŸŽ', '๐Ÿ“', '๐Ÿ‘', '๐Ÿ‹']
fruits.splice(1, 1, '๐Ÿ', '๐Ÿ‰');
console.log(fruits); // ['๐ŸŽ', '๐Ÿ', '๐Ÿ‰', '๐Ÿ‘', '๐Ÿ‹']
fruits.splice(1, 0, '๐Ÿ', '๐Ÿ‰'); // item ์ง€์šฐ์ง€ ์•Š๊ณ  ํ•ด๋‹น ์ž๋ฆฌ์— item ์‚ฝ์ž…
console.log(fruits); // ['๐ŸŽ', '๐Ÿ', '๐Ÿ‰', '๐Ÿ“', '๐Ÿ‘', '๐Ÿ‹']

// combine two arrays
const fruits2 = ['๐Ÿ','๐Ÿฅฅ'];
const newFruits = fruits.concat(fruits2);
console.log(newFruits); // ['๐ŸŽ', '๐Ÿ', '๐Ÿ‰', '๐Ÿ‘', '๐Ÿ‹', '๐Ÿ','๐Ÿฅฅ']
  • shift, unshift๋Š” push, pop๋ณด๋‹ค ํ›จ์”ฌ ๋А๋ฆฌ๋‹ค.
  • ๋งจ ์•ž์—์„œ add, remove๊ฐ€ ์ผ์–ด๋‚˜๊ธฐ ๋•Œ๋ฌธ์— ๊ธฐ์กด์— ์žˆ๋˜ item๋“ค์˜ ์œ„์น˜๋ฅผ ๊ณ„์†ํ•ด์„œ ์˜ฎ๊ฒจ์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ!

 

5. Searching

// indexOf: find the index
console.log(fruits); // ['๐ŸŽ', '๐Ÿ', '๐Ÿ‰', '๐Ÿ‘', '๐Ÿ‹']
console.log(fruits.indexOf('๐ŸŽ')); // 0
console.log(fruits.indexOf('๐Ÿ‰')); // 2
console.log(fruits.indexOf('๐Ÿฅฅ')); // -1

// includes
console.log(fruits.includes('๐Ÿ‰')); // true
console.log(fruits.includes('๐Ÿฅฅ')); // false

// lastIndexOf
fruits.push('๐ŸŽ');
console.log(fruits); // ['๐ŸŽ', '๐Ÿ', '๐Ÿ‰', '๐Ÿ‘', '๐Ÿ‹', '๐ŸŽ']
console.log(fruits.indexOf('๐ŸŽ')); // 0
console.log(fruits.lastIndexOf('๐ŸŽ')); // 5

 

 

 

์ฐธ๊ณ 

youtube ๋“œ๋ฆผ์ฝ”๋”ฉ by ์—˜๋ฆฌ

https://www.youtube.com/watch?v=yOdAVDuHUKQ&list=PLv2d7VI9OotTVOL4QmPfvJWPJvkmv6h-2&index=8