[JS] ๋ฐฐ์—ด/for

XIXIยท2022๋…„ 6์›” 9์ผ
0

๋Œ€๊ตฌAI์Šค์ฟจ

๋ชฉ๋ก ๋ณด๊ธฐ
31/57
post-thumbnail

๐ŸŒฑ ๋ฐฐ์—ด

โœ๏ธ๊ธฐ๋ณธ

๋ฐฐ์—ด, ์—ฌ๋Ÿฌ ์ž๋ฃŒ๋“ค ๋ฌถ์–ด์„œ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ž๋ฃŒ๋“ค

์š”์†Œ ์—˜๋ฆฌ๋จผํŠธ element
const array = [237, "๋ฌธ์ž", false, [27, 45], {}, function()]

const str ="์•ˆ๋…•ํ•˜์„ธ์š”" : ๋ณ€์ˆ˜์— ๋ฌธ์ž๋ฅผ ๋‹ด์€ ๊ฒฝ์šฐ
str.length : ๋ณ€์ˆ˜๋‹ด์€ ๋ฌธ์ž๋ฐ์ดํ„ฐ ๊ธธ์ด
str[3] : ๋ณ€์ˆ˜์— ๋‹ด๊ธด ๋ฌธ์ž ๋ฐ์ดํ„ฐ index

๋ฐฐ์—ด ์š”์†Œ๋Š” ์ˆซ์žํ˜•, ๋ฌธ์žํ˜•, Boolean, ๋ฐฐ์—ด, ํ•จ์ˆ˜๊ฐ€ ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋‹ค.

โœ๏ธ์š”์†Œ ์ถ”๊ฐ€

push

๋ฐฐ์—ด ์•ˆ ๋งˆ์ง€๋ง‰์— ์š”์†Œ ์ถ”๊ฐ€

const fr = ["ํ‚ค์œ„", "๋”ธ๊ธฐ", "์‚ฌ๊ณผ"]
fr.push("๋ฐ”๋‚˜๋‚˜")

push ํ•  ๊ฒฝ์šฐ ๋ฐฐ์—ด ๋งˆ์ง€๋ง‰์— ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฝ์ž…ํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ฐฐ์—ด์ด๋ฆ„[๋ฐฐ์—ด์ด๋ฆ„.length]

๋ฐฐ์—ด์˜ ๋์— ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ• (2) : ๋ฐฐ์—ด์ด๋ฆ„[๋ฐฐ์—ด์ด๋ฆ„.length]

var arr = ['a', 'b', 'c']
arr[arr.length] = 'd'

a, b, c, d ์ˆœ์œผ๋กœ ๋งˆ์ง€๋ง‰์— ๋ฐ์ดํ„ฐ๊ฐ€ ์‚ฝ์ž…๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Œ.

unshift

var arr = ['a', 'b', 'c'];
arr.unshift('d');

๋ฐฐ์—ด ๋งจ ์•ž์— ์š”์†Œ ์ถ”๊ฐ€

๋ฐฐ์—ด์ด๋ฆ„[n]

var arr = ['a', 'b', 'c']
arr[5] = 'd'

5๋ฒˆ์งธ ์š”์†Œ์— d๊ฐ€ ์ถ”๊ฐ€๋Œ.
['a', 'b', 'c', 'undefined', 'undefined', 'd']

splice('์œ„์น˜๊ฐ’n', 0, '์š”์†Œ')

var arr = ['a', 'b', 'c'];
arr.splice(1, 0, 'd');

splice(1๋ฒˆ์งธ index, ๊ทธ ์ž๋ฆฌ์—, d๋ฅผ ๋„ฃ๋Š”๋‹ค)

โœ๏ธindexOf

์š”์†Œ์˜ index ์œ„์น˜๋ฅผ ์™ผ์ชฝ์—์„œ๋ถ€ํ„ฐ ์ฐพ์•„์คŒ

const fr = ["ํ‚ค์œ„", "๋”ธ๊ธฐ", "์‚ฌ๊ณผ"]
fr.push("๋ฐ”๋‚˜๋‚˜")
fr.indexOf("์‚ฌ๊ณผ")

์‚ฌ๊ณผ ์š”์†Œ index๋ฅผ ์ฐพ์•„์ค€๋‹ค

โœ๏ธ์š”์†Œ ์ œ๊ฑฐ

splice

์ง€์ •ํ•œ ์œ„์น˜์— ์žˆ๋Š” ์š”์†Œ(n๊ฐœ)๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ๋ฐฉ๋ฒ• splice('์œ„์น˜๊ฐ’n', '์ œ๊ฑฐํ•  ์š”์†Œ์˜ ๊ฐฏ์ˆ˜')

const fr = ["ํ‚ค์œ„", "๋”ธ๊ธฐ", "์‚ฌ๊ณผ"]
fr.push("๋ฐ”๋‚˜๋‚˜")
fr.indexOf("์‚ฌ๊ณผ")
fr.splice(2,1)

splice๋กœ ์š”์†Œ์˜ index ์œ„์น˜ ๊ธฐ์ค€์œผ๋กœ 1๊ฐœ ์š”์†Œ๊ฐ€ ์‚ญ์ œ๋œ ๊ฒƒ ํ™•์ธ.

pop

var arr = ['a', 'b', 'c', 'd', 'e'];
arr.pop();

๋งจ ๋’ค์— ์š”์†Œ๋ฅผ ์‚ญ์ œํ•œ ๋ง.

๋ฐฐ์—ด์ด๋ฆ„.length-1

var arr = ['a', 'b', 'c', 'd', 'e'];
arr.length = arr.length -1;

๋งˆ์ง€๋ง‰ ์š”์†Œ ์ œ๊ฑฐ ๊ฐ€๋Šฅ

shift()

var arr = ['a', 'b', 'c', 'd', 'e'];
arr.shift();

์ฒซ๋ฒˆ์งธ ์š”์†Œ ์ œ๊ฑฐ

splice('์œ„์น˜๊ฐ’n', '์ œ๊ฑฐํ•  ์š”์†Œ์˜ ๊ฐฏ์ˆ˜')

var arr = ['a', 'b', 'c', 'd', 'e'];
arr.splice(1, 1);

์ง€์ •ํ•œ ์œ„์น˜์— ์žˆ๋Š” ์š”์†Œ(n๊ฐœ)๋ฅผ ์ œ๊ฑฐ

delete

var arr = ['a', 'b', 'c', 'd', 'e'];
delete arr[1];

์š”์†Œ๋“ค์€ ๊ทธ๋Œ€๋กœ ๋‘๊ณ  ํ•ด๋‹น index ์œ„์น˜์— ๊ฐ’์„ ์‚ญ์ œํ•จ

๐ŸŒฑ for

โœ๏ธfor

<script>
    const fr = ["์‚ฌ๊ณผ", "๋”ธ๊ธฐ", "์ˆ˜๋ฐ•", "์ฐธ์™ธ"]
    for (const i in fr) {
      console.log(fr[i]);
    }
</script>
</body>
  • const i in fr fr์ด๋ผ๋Š” ๋ฐฐ์—ด ์•ˆ์— ์žˆ๋Š” ๊ฐœ์ˆ˜๋งŒํผ ๋ฐ˜๋ณต

๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ๋ฐฐ์—ด ์•ˆ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ๋‹ค

<script>
    const fr = ["์‚ฌ๊ณผ", "๋”ธ๊ธฐ", "์ˆ˜๋ฐ•", "์ฐธ์™ธ"]
    for (const i in fr) {
      console.log(`${i} ๋ฒˆ์งธ ์ธ๋ฑ์Šค์˜ ๊ณผ์ผ์€ ${fr[i]}`);
    }
</script>

์ธ๋ฑ์Šค, ์š”์†Œ, ๋ฌธ์ž๋ฅผ ์กฐํ•ฉํ•ด for๋ฌธ์œผ๋กœ ๋ฌธ์žฅ์„ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค

๐Ÿƒ ์–ด๋ ค์› ๋˜ ์  or ํ•ด๊ฒฐ๋ชปํ•œ ๊ฒƒ

๋ฐ˜๋ณต๋ฌธ ํ™œ์šฉ๋ฐฉ๋ฒ•์ด ๊ถ๊ธˆํ•˜๋‹ค.

๐Ÿ€ ํ•ด๊ฒฐ๋ฐฉ๋ฒ• ์ž‘์„ฑ

โœ๏ธ ์–ด๋–ป๊ฒŒ ํ•ด๊ฒฐ์„ ํ–ˆ๋Š”๊ฐ€?
โœ๏ธ ์ด๋ ‡๊ฒŒ ์ดํ•ด๋ฅผ ํ–ˆ๋‹ค
โœ๏ธ ์–ด๋””๊นŒ์ง€ ์ดํ•ดํ–ˆ์ง€?
โœ๏ธ ๋‹ค์Œ์— ์‹œ๋„ํ•ด๋ณผ ๋ฐฉ๋ฒ•
๋ฐ˜๋ณต๋ฌธ์„ ์‹ค๋ฌด์—์„œ ์–ด๋–ป๊ฒŒ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ์ฐพ์•„๋ด์•ผ๊ฒ ๋‹ค

๐ŸŒท ํ•™์Šต ์†Œ๊ฐ

๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ๋ฌธ์žฅ์„ ๋ฐ˜๋ณตํ•ด์„œ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์—ˆ์Œ.

profile
Life is experience:)

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