const numbers = [1,2,3,4]
console.log(numbers.length) // 4
const numbers = [1,2,3,4]
console.log(numbers.find((num) => num > 2) //3
// num > 2 ์ ์กฐ๊ฑด์ ๋ง์กฑํ๋ ์์๊ฐ ์ฌ๋ฌ๊ฐ ์ด์ง๋ง ์ฒซ๋ฒ์งธ ๊ฐ๋ง ๋ฐํ ๋จ
const numbers = [1,2,3,4]
console.log(numbers.findIndex((num) => num > 2) //2
// 2๋ณด๋ค ํฐ ์์ ์ค ์ฒซ๋ฒ์งธ ์์์ธ 3์ index๊ฐ => 2
const numbers = [1,2,3,4]
console.log(numbers.includes(3)) //true
console.log(numbers.includes(6)) //false
const abc = ['a', 'b', 'c']
const def = ['d', 'e', 'f']
console.log(abc.concat(def)) //['a', 'b', 'c', 'd', 'e', 'f']
const fruits = ["Apple","Banana","Cherry"]
fruits.forEach((item, idx, arr) => {
console.log(item, idx, arr)
});
/*
์ถ๋ ฅ๊ฐ
Apple 0 [ 'Apple', 'Banana', 'Cherry' ]
Banana 1 [ 'Apple', 'Banana', 'Cherry' ]
Cherry 2 [ 'Apple', 'Banana', 'Cherry' ]
*/
undefined
๋ฅผ ๋ฐํํ๋ค const fruits = ["Apple","Banana","Cherry"]
const fruit = fruits.forEach((item) => {
return item
});
console.log(fruit) //undefined
const fruits = ["Apple","Banana","Cherry"]
const a = fruits.map((item,idx) => (
return {
item: item,
index: idx
}
))
console.log(a)
/*
๋ฐํ๊ฐ
[
{ item: 'Apple', index: 0 },
{ item: 'Banana', index: 1 },
{ item: 'Cherry', index: 2 }
]
*/
forEach()์ map()์ ๊ฐ์ฅ ํฐ ์ฐจ์ด๋ returnํ๋ ๊ฐ์ด ์๋๋ ์๋๋๋ก ๋๋ ์ ์๋ค
const numbers = [1,2,3,4]
numbers.reduce((acc,cur) => acc + cur) // 10
// 1 + 2 + 3 + 4 = 10 ๋ฐํ
๋ธ๋ก๊ทธ์ reduce์ ๋ํด ๋ ์์ธํ ๊ธฐ์ ํ ํฌ์คํธ๊ฐ ์์ต๋๋ค. Click!
const numbers = [1,2,3,4]
//map์ ๊ฒฝ์ฐ ๋น๊ต์ฐ์ฐ์๋ก ์กฐ๊ฑด์ ์์ฑ ์ boolean๋ฐ์ดํฐ๋ก ๊ฐ์ด ๋ฐํ ๋จ
const b = numbers.map((num) => num < 3)
console.log(b) //[ true, true, false, false ]
//filter์ ๊ฒฝ์ฐ true๊ฐ์ผ๋ก ๋์ํ๋ ์์๋ฅผ ๋ฐฐ์ด์ ๋ด์ ๋ฐํ
const c = numbers.filter((num) => num < 3)
console.log(c) //[ 1, 2 ]
const arr = [1,2,3,4,5]
arr.every((item) => item > 3) // false
arr.every((item) => item < 6) //true
const arr = [1,2,3,4,5]
arr.some((item) => item > 3) // true
arr.some((item) => item > 6) // false
Array.from()
: ์ ์ฌ๋ฐฐ์ด์ ์ค์ ๋ฐฐ์ด๋ก ๋ฐ๊ฟ์ฃผ๋ ์ ์ ๋ฉ์๋Array.isArray()
: ํด๋น ์์๊ฐ ๋ฐฐ์ด์ธ์ง ์๋์ง boolean๊ฐ์ ๋ฐํ// querySelectorAll() ๋ก ์ ํํด ์จ ์์๋ค์ ์ ์ฌ๋ฐฐ์ด
const liEls = document.querySelectorAll('li');
console.log(liEls); //NodeList(8) [li, li, li, li, li, li, li, li]
console.log(Array.isArray(liEls)) //false
// Array.from() ์ ํตํด ์ค์ ๋ฐฐ์ด๋ก ์ ํ
const arrLiEls = Array.from(liEls);
console.log(arrLiEls); //Array(8) [li, li, li, li, li, li, li, li]
console.log(Array.isArray(arrLiEls)) //true
const numbers = [1,2,3,4]
numbers.push(5)
console.log(numbers) // [1, 2, 3, 4, 5]
const numbers = [1,2,3,4]
console.log(numbers.pop()) //4
console.log(numbers) // [1, 2, 3]
const numbers = [1,2,3,4]
console.log(numbers.pop()) //1
console.log(numbers) // [2, 3, 4]
const numbers = [1,2,3,4]
numbers.unshift(0)
console.log(numbers) // [0, 1, 2, 3, 4]
const numbers = [4, 17, 21, 103, 1, 0, 78]
// ์ ๋์ฝ๋ ํฌ์ธํธ ์์ ์ ๋ ฌ (์ซ์ ์์๋ฆฌ ํฌ๊ธฐ ์)
numbers.sort()
console.log(numbers) // [0, 1, 103, 17, 21, 4, 78]
//a - b ์ ๊ฒฐ๊ณผ๊ฐ์ด ์์๋ฉด a๊ฐ ๋ ํฐ ์ , ์์๋ฉด b๊ฐ ๋ ํฐ์ ๋ผ๋ ๋ฒ์น์ ํตํด ์ซ์๋ฅผ ํฌ๊ธฐ์ ์ ๋ ฌ
numbers.sort((a,b) => a - b)
console.log(numbers) // [0, 1, 4, 17, 21, 78, 103]
// b - a๋ก ๋ค์ง์ผ๋ฉด ํฐ์๋ถํฐ ์์ ์ ์์๋ก ์ ๋ ฌ
numbers.sort((a, b) => b - a)
console.log(numbers) // [103, 78, 21, 17, 4, 1, 0]
const numbers = [1,2,3,4]
numbers.reverse()
console.log(numbers) //[4, 3, 2, 1]
const numbers = [1,2,3,4]
//๊ธฐ๋ณธ ์ฌ์ฉ๋ฒ
numbers.splice(2,1); //2๋ฒ ์์ดํ
๋ถํฐ 1๊ฐ์ ์์ดํ
์ญ์
console.log(numbers) //[ 1, 2, 4 ]
//ํ์ฌ numbers ์ ๊ฐ = [ 1, 2, 4 ]
//์ญ์ ํ ์์์ ๊ฐฏ์๋ฅผ 0์ผ๋ก ์ง์ ํ๋ฉด ์ญ์ ๋๋ ์์ ์์ด ์์ดํ
์ถ๊ฐ ๊ฐ๋ฅ
numbers.splice(2, 0, 3); //2๋ฒ ์์ดํ
๋ถํฐ 0๊ฐ์ ์์ดํ
์ญ์ , 3 ์ถ๊ฐ
console.log(numbers) //[ 1, 2, 3, 4 ]
//ํ์ฌ numbers ์ ๊ฐ = [ 1, 2, 3, 4 ]
//ํ๋์ ์์ดํ
์ ์ ๊ฑฐํ๊ณ ํ๋์ ์์ดํ
์ ์ถ๊ฐํ์ฌ ํด๋น ์์๋ฅผ ๊ต์ฒด
numbers.splice(2, 1, 0); //2๋ฒ ์์ดํ
๋ถํฐ 1๊ฐ์ ์์ดํ
์ญ์ , 0 ์ถ๊ฐ
console.log(numbers) //[ 1, 2, 0, 4 ]
์๊ณ ๋ ์์ง๋ง ์ต์ํ์ง ์์์ ์ฌ์ฉํด ๋ณธ ์ ์ด ์์๋ ๋ฉ์๋๋ค์ ์ด๋ฒ ๊ธฐํ์ ๋ค์ํ๋ฒ ์ฌ์ฉ๋ฒ์ ์ตํ๋ฉฐ ๋ฆฌ๋ง์ธ๋ ๋์๊ณ , ์ผ๋ถ ๋ฉ์๋๋ค์ ์ตํ ์ฌ์ฉํ๋ ๊ฒ์์๋ ์๋กญ๊ฒ ๋ค๊ฐ์๋ค.
ํนํ forEach์ map๊ฐ ์ด๋ค ์ํฉ์์ ์ฐ์ด๋์ง ๊ทธ ์ฐจ์ด๋ฅผ ๊ทธ๋ฅ ๋๋์ (..)์ผ๋ก๋ง ์๊ณ ์์๋๋ฐ ์ ํํ ์ฐจ์ด์ ์ ์ฒ์์ผ๋ก ์ธ์งํ๋ค...!!
๊ฐ๋ฐ์์๊ฒ ์? ์ด๋ป๊ฒ? ๋ผ๋ ๊ถ๊ธ์ฆ์ ๊ณ์ํด์ ํ๊ณ ๋ค๋
์ผ ํ๋ ์ง๋ฌธ์ธ๊ฒ์ ๊ฐ๊ณผํ๊ณ , ๋๋์ ์ธ ๋๋์ผ๋ก๋ง ์ตํ๊ณ ์ฌ์ฉํ๊ณ ์์๋ ๊ฒ์ ์๋๊น..ํ๋ฉฐ ๋๋ฅผ ๋๋์๋ณด๊ฒ ๋๋ค...๐ถโ๐ซ๏ธ