[JavaScript]๋ฐฐ์—ด, ๊ฐ์ฒด๐ŸŽ

miniยท2022๋…„ 12์›” 27์ผ
0

Javascript๋ฝ€๊ฐœ๊ธฐ

๋ชฉ๋ก ๋ณด๊ธฐ
1/7
post-thumbnail

ย ๋ฐฐ์—ด(Array)

์—ฌ๋Ÿฌ๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆœ์„œ์™€ ํ•จ๊ป˜ ์ €์žฅํ•˜๊ณ  ์‹ถ์„๋•Œ ์‚ฌ์šฉ ํ•˜๋Š” ์ €์žฅ ์ฐฝ๊ณ 

๋ฐฐ์—ด ๋งŒ๋“ค๊ธฐ

let minArray = ["์ด๊ฒƒ","๊ฐ๊ฐ ","ํ•˜๋‚˜๋ฅผ,"์š”์†Œ"]

๋Œ€๊ด„ํ˜ธ ์•ˆ์— ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ชจ์•„์„œ ๋„ฃ์–ด์ฃผ์‹œ๋ฉด ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด ์ฃผ์‹  ๊ฒƒ ์ž…๋‹ˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ , ๋ฐฐ์—ด ์•ˆ์˜ ๋ฐ์ดํ„ฐ๋“ค ํ•˜๋‚˜ํ•˜๋‚˜๋ฅผ ์š”์†Œ(element) ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

๋ฐฐ์—ด์˜ index

์œ„์—์„œ ๋ฐฐ์—ด์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆœ์„œ์™€ ํ•จ๊ป˜ ์ €์žฅ
๋ฐฐ์—ด์˜ index ๋Š” ๊ฐ ์š”์†Œ์— ์ˆœ์„œ๋ฅผ ๋ถ€์—ฌ ํ•  ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ํ•ด๋‹น ์š”์†Œ์— ์ ‘๊ทผ์ด ๊ฐ€๋Šฅ

index ๋ฒˆํ˜ธ๋Š” ํŠน์ดํ•˜๊ฒŒ 0๋ฒˆ ๋ถ€ํ„ฐ ์‹œ์ž‘ ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์•„๋ž˜์ฒ˜๋Ÿผ

let minArray = ["์ด๊ฒƒ","๊ฐ๊ฐ ","ํ•˜๋‚˜๋ฅผ,"์š”์†Œ"]
      		     0     1       2     3

์ธ๋ฑ์Šค ๋ฒˆํ˜ธ๋กœ ์ ‘๊ทผ

๋ฒˆํ˜ธ๋กœ ์š”์†Œ์— ์ ‘๊ทผํ•˜๊ธฐ
minArray[2] = "ํ•˜๋‚˜๋ฅผ"
minArray[3] = "์š”์†Œ"

๋ฐฐ์—ด์˜ property


**length ์†์„ฑ์€ ์ด๋ฆ„์—์„œ ์•Œ ์ˆ˜ ์žˆ ๋“ฏ ๋ฐฐ์—ด์˜ ๊ธธ์ด ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์†์„ฑ**

let marray = ["์•„์นจ์€","๋“ ๋“ ํ•˜๊ฒŒ","๋จน์–ด์•ผ","์˜ค์ „์—…๋ฌด๊ฐ€","์ž˜๋œ๋‹ค"]
// index 0 1 2 3 4
// length 1 2 3 4 5 -> ๋”ฐ๋ผ์„œ arr.length = 5

๋ฐฐ์—ด์˜ ๋ฉ”์†Œ๋“œ(Method)

๋ฉ”์„œ๋“œ๋Š” ์–ด๋– ํ•œ ๊ธฐ๋Šฅ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ช…๋ น์–ด , ๋ฐฐ์—ด์— ๋‚ด์žฅ๋˜์–ด ์žˆ๋Š” ๊ธฐ๋Šฅ
๋ฐฐ์—ด์˜ ๋ฉ”์„œ๋“œ์—๋Š” ๋‹ค์–‘ํ•œ ๋ฉ”์„œ๋“œ๋“ค์ด ์กด์žฌ, ์ƒํ™ฉ์— ๋”ฐ๋ผ MDN์— ๊ฒ€์ƒ‰์œผ๋กœ ์ ์ ˆํžˆ ์‚ฌ์šฉ

array.push() : ๋ฐฐ์—ด์˜ ๊ฐ€์žฅ ๋’ค์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€

push(โ€์ถ”๊ฐ€ํ•  ๋ฐ์ดํ„ฐโ€) push์˜ ๊ด„ํ˜ธ ์•ˆ์—๋Š” ์ถ”๊ฐ€ ํ•  ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ๋Š”๋‹ค.

let food = ["ํ”ผ์ž","ํ”ผํ‚จ","ํ–„๋ฒ„๊ฑฐ"]
food.push("ํŒŒ์Šคํƒ€")
// ๊ฒฐ๊ณผ
food = [""ํ”ผ์ž","์น˜ํ‚จ","ํ–„๋ฒ„๊ฑฐ","ํŒŒ์Šคํƒ€"]

array.pop() : ๋ฐฐ์—ด์˜ ๊ฐ€์žฅ ๋’ค์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œ

push์™€๋Š” ๋‹ค๋ฅด๊ฒŒ pop์˜ ๊ด„ํ˜ธ ๋น„์›Œ๋„ ๊ฐ€๋Šฅ
let food = ["ํ”ผ์ž","์น˜ํ‚จ","ํ–„๋ฒ„๊ฑฐ"]
food.pop()
// ๊ฒฐ๊ณผ
food = ["ํ”ผ์ž","์น˜ํ‚จ"]

array.includes() : ํŠน์ • ๋ฐฐ์—ด์— ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ๊ฐ€ ํฌํ•จ๋˜์–ด์žˆ๋Š”์ง€ ํ™•์ธ

ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€์— ๋”ฐ๋ผ boolean๊ฐ’์„ ๋ฐ˜ํ™˜

let food = ["ํ”ผ์ž","์น˜ํ‚จ","ํ–„๋ฒ„๊ฑฐ"]
food.includes("ํ”ผ์ž")
// ๊ฒฐ๊ณผ
true

array.indexOf() : ํŠน์ • ๋ฐฐ์—ด์—์„œ ์ง€์ •๋œ ์š”์†Œ๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ๋Š” ์ฒซ ๋ฒˆ์งธ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜

let food = ["ํ”ผ์ž","์น˜ํ‚จ","ํ–„๋ฒ„๊ฑฐ"]
food.indexOf("ํ”ผ์ž")
// ๊ฒฐ๊ณผ
0

๊ฐ์ฒด(object)๐Ÿ”—

์ฒด๋Š” ํ‚ค๊ฐ’์„ ํ†ตํ•ด ํ•ด๋‹น ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฌด์—‡์— ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ์ธ์ง€ ๋ช…์‹œ

const min = ["๋‚˜๋Š”", 160,180,"์‚ฌ์ด"]

์—ฌ๊ธฐ์„œ ์ˆซ์ž์— ๋Œ€ํ•œ ๊ฐ’์ด ์•Œ ์ˆ˜๊ฐ€ ์—†๋‹ค.

ํ‚ค๊ฐ€ ๋  ์ˆ˜๋„ ์žˆ๊ณ , ๋ชธ๋ฌด๊ฒŒ๋‚˜ ๋‚˜์ด๊ฐ€ ๋ ์ˆ˜๋„ ์žˆ๋‹ค.

๋ฐฐ์—ด์€ ์•ˆ์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋“ค์ด ๋ฌด์—‡์— ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ์ธ์ง€ ๋ช…์‹œ ๋ถˆ๊ฐ€๋Šฅ ํ•˜๋‹ค.

๊ฐ์ฒด ๋งŒ๋“ค๊ธฐ

๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๋•Œ๋Š” ์ค‘๊ด„ํ˜ธ: {} ๋ฅผ ์ด์šฉ

let MinData = {
name:"min",
age:30,
height:160,
company:"๋ฏธ๋‹ˆ๋ฏธ๋‹ˆ"
}

์ค‘๊ด„ํ˜ธ ์•ˆ์— ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ชจ์•„์„œ ๋„ฃ์–ด์ฃผ์‹œ๋ฉด ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ ๋‹ค.

๊ทธ๋ฆฌ๊ณ , ๊ฐ์ฒด ์•ˆ์˜ ๋ฐ์ดํ„ฐ๋“ค ํ•˜๋‚˜ํ•˜๋‚˜๋ฅผ ํ”„๋กœํผํ‹ฐ(property) ๋ผ๊ณ  ํ•œ๋‹ค.

{name:"min"}
name - key : "min" - value

์™ผ์ชฝ์—๋Š” key๊ฐ’ ์ด ์˜ค๋ฅธ์ชฝ์—๋Š” value๊ฐ’

๊ทธ๋ฆฌ๊ณ  key์™€ value๋ฅผ ๋ฌถ์€ ๊ฒƒ์„ property ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

๊ฐ์ฒด ํ”„๋กœํผํ‹ฐ(property)

๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ์— ์ ‘๊ทผํ•  ๋•Œ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ key๊ฐ’์— ์ ‘๊ทผํ•ด์„œ value๋ฅผ ๊ฐ€์ง€๊ณ  ์˜ต๋‹ˆ๋‹ค.

๋˜ํ•œ ํ”„๋กœํผํ‹ฐ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์—๋Š” ๋‘๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค.

Dot notation(์  ํ‘œ๊ธฐ๋ฒ•)

key๊ฐ’์— ์ ‘๊ทผ์‹œ, ์ ์„ ์ด์šฉํ•ด์„œ ์ ‘๊ทผ

let MinData = {
	name:"min",
	age:30,
	height:160,
	company:"๋ฏธ๋‹ˆ๋ฏธ๋‹ˆ"
}

//Dot notation(์  ํ‘œ๊ธฐ๋ฒ•) ์ ‘๊ทผ
MinData.name = "min"
MinData.age = 30

Bracket notation(๊ด„ํ˜ธ ํ‘œ๊ธฐ๋ฒ•)

key๊ฐ’์— ์ ‘๊ทผ์‹œ, ๊ด„ํ˜ธ[]๋ฅผ ์ด์šฉํ•ด์„œ ์ ‘๊ทผ

//Bracket notation(๊ด„ํ˜ธ ํ‘œ๊ธฐ๋ฒ•) ์ ‘๊ทผ
MinData["name"] = "min"
MinData["age"] = 30

โ—๏ธ์ฃผ์˜โ—๏ธ
Bracket notation์„ ์‚ฌ์šฉํ•˜์‹ค ๋•Œ โ€œโ€๋ฅผ ๋ถ™์—ฌ์ฃผ์‹œ์ง€ ์•Š์œผ์‹œ๋ฉด, ์•ˆ์˜ key๊ฐ’์ด ์•„๋‹Œ ๋ณ€์ˆ˜๋กœ ์ธ์‹ key ๊ฐ’์— ์ ‘๊ทผ์„ ์›ํ•˜์‹ ๋‹ค๋ฉด ๋ฐ˜๋“œ์‹œ โ€œโ€๋ฅผ ๋ถ™์ธ๋‹ค.

๊ฐ์ฒด(object) Method

๋ฐฐ์—ด๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ฐ์ฒด์—๋„ ๋‚ด์žฅ๋œ ๋ฉ”์„œ๋“œ

์ด๊ฒƒ ๋˜ํ•œ ๋‹ค์–‘ํ•œ ๋ฉ”์„œ๋“œ๋“ค์ด ์กด์žฌ, ์ƒํ™ฉ์— ๋”ฐ๋ผ MDN์— ๊ฒ€์ƒ‰ํ•˜์…”์„œ ์ ์ ˆํžˆ ์‚ฌ์šฉ

Object.keys()

๊ฐ์ฒด์˜ key๋งŒ์„ ๊ฐ€์ ธ์™€ ๋ฐฐ์—ด์— ๋‹ด์•„์ฃผ๋Š” ๋ฉ”์„œ๋“œ

์‚ฌ์šฉํ•˜์‹ค๋•Œ๋Š” ๊ด„ํ˜ธ ์•ˆ์— key๊ฐ’์„ ๋ฝ‘์•„์˜ฌ ๊ฐ์ฒด ์ด๋ฆ„์„ ๋„ฃ์–ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

let MinData = {
	name:"min",
	age:30,
	height:160,
	company:"๋ฏธ๋‹ˆ๋ฏธ๋‹ˆ"
}

Object.keys(MinData)
// ๊ฒฐ๊ณผ
[ "name", "age", "height", "company"]

Object.values()

๊ฐ์ฒด์˜ value๋งŒ์„ ๊ฐ€์ ธ์™€ ๋ฐฐ์—ด์— ๋‹ด์•„์ฃผ๋Š” ๋ฉ”์„œ๋“œ

์‚ฌ์šฉ ์‹œ ๊ด„ํ˜ธ ์•ˆ์— value๊ฐ’์„ ๋ฝ‘์•„์˜ฌ ๊ฐ์ฒด ์ด๋ฆ„์„ ๋„ฃ๋Š”๋‹ค.

let MinData = {
	name:"min",
	age:30,
	height:160,
	company:"๋ฏธ๋‹ˆ๋ฏธ๋‹ˆ"
}

Object.values(MinData)
// ๊ฒฐ๊ณผ
[ "min", "30", "160", "๋ฏธ๋‹ˆ๋ฏธ๋‹ˆ"]
profile
ํ•  ์ˆ˜ ์žˆ๋‹ค!

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