- ์๋ฐ์คํฌ๋ฆฝํธ ๋ฐฐ์ด์ ํน์ง
1. ์ด๋ค ์ข ๋ฅ์ ์๋ฃํ๋ ํ๋์ ๋ฐฐ์ด์ ๋ด์ ์ ์์
<body>
<script>
//์ด๋ ํ ์ข
๋ฅ์ ์๋ฃํ๋ ํ๋์ ๋ฐฐ์ด์ ๋ด์ ์ ์์
let arr = [35, "a", 37.1, true, {}];
console.log(arr);
</script>
</body>
2. push๋ฅผ ์ด์ฉํ์ฌ ๋ฐฐ์ด ์์๋ฅผ ๋์ค์ ์ถ๊ฐํ ์ ์์
<body>
<script>
let arr = [35, 45, 55, 65, 75];
//๋ฐฐ์ด ์์ ์ถ๊ฐ : push ์ฌ์ฉ
arr.push(75);
arr.push(85);
for (let i of arr) {
console.log(i);
}
</script>
</body>
3. ๋ฐฐ์ด์ ๋จผ์ ์ ์ธํ ํ ์ฌ์ฉํ ์ ์์
- ๋ฐฐ์ด ๋ฉ์๋ ์ฌ์ฉ๊ฐ๋ฅ
- .join() : ์ฐ๊ฒฐ
- .concat() : ๋ค๋ฅธ ๋ฐฐ์ด๊ณผ ์ฐ๊ฒฐ
- .slice() : ์์ ์๋ฅด๊ธฐ
- .reverse() : ๋ฐฐ์ด ๊ฑฐ๊พธ๋ก ์ถ๋ ฅ
- .sort() : ๋ฐฐ์ด์ ์๋ ๋ฐ์ดํฐ ์ ๋ ฌ
<body>
<script>
let arr = new Array();
//๋ฐฐ์ด ์์ ์ถ๊ฐ : push ์ฌ์ฉ
arr.push(75);
arr.push(85);
arr.push(35);
arr.push(45);
arr.push(55);
arr.push(65);
for (let i of arr) {
document.write(i + "<br>");
}
document.write("<br><br>");
document.write("์ฐ๊ฒฐ : " + arr.join("-") + "<br>");
document.write(
"๋ค๋ฅธ ๋ฐฐ์ด๊ณผ ์ฐ๊ฒฐ : " + arr.concat([2, 4, 6, 8, 10]) + "<br>"
);
document.write("์์ ์๋ฅด๊ธฐ : " + arr.slice(1, 3) + "<br>");
document.write("๋ฐฐ์ด ๊ฑฐ๊พธ๋ก ์ถ๋ ฅ : " + arr.reverse() + "<br>");
document.write("๋ฐ์ดํฐ ์ ๋ ฌํ๊ธฐ : " + arr.sort());
</script>
</body>
- ๋ธ๋ผ์ฐ์ ์ถ๋ ฅ