๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
JavaScript

๊ฐ์ฒด์™€ ๋ฐฐ์—ด ๋ฏธ๋ฆฌ๋ณด๊ธฐ

by chanfficial 2023. 3. 5.

๐Ÿ’ก ๊ฐ์ฒด์™€ ๋ฐฐ์—ด์€ ์›์‹œํƒ€์ž…์ด ์•„๋‹Œ ์ฐธ์กฐ (reference) ํƒ€์ž…

  • ์•ž์„œ ๋‹ค๋ฃฌ ์ž๋ฃŒํ˜•๋“ค์€ ์›์‹œ (primitive) ํƒ€์ž…

1. ๊ฐ์ฒด ๋ฏธ๋ฆฌ๋ณด๊ธฐ

  • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ์›์‹œ ํƒ€์ž…์ด ์•„๋‹Œ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋Š” ๊ทผ๋ณธ์ ์œผ๋กœ ๊ฐ์ฒด
  • ๋ณตํ•ฉ์ ์ธ ์ •๋ณด๋ฅผ ํ”„๋กœํผํ‹ฐ(property)๋กœ ์ €์žฅํ•˜๋Š” ์ž๋ฃŒํ˜• - ํ‚ค์™€ ๊ฐ’์˜ ์กฐํ•ฉ
const objName = {
  key1: value1,
  key2: value2,
  ...
};
// โš ๏ธ ๋ธ”๋ก์ด ์•„๋‹˜!
const person1 = {
  name: '๊น€์ฒ ์ˆ˜',
  age: 25,
  married: false
};

console.log(typeof person1); // object
console.log(person1); // {name: '๊น€์ฒ ์ˆ˜', age: 25, married: false}

๊ฐ์ฒด๊ฐ€ ๋‹ด๊ณ ์žˆ๋Š” ๋งŽ์€ ์ •๋ณด๋“ค์„ ์ถœ๋ ฅํ•จ

 

(1) ํ”„๋กœํผํ‹ฐ ์ ‘๊ทผ

 

๐Ÿ’ก ์†์„ฑ๊ฐ’์— ์ ‘๊ทผํ•˜๋Š” ๋‘ ๋ฐฉ๋ฒ•

console.log(
  person1.name,
  person1.age,
  person1.married
); // ๊น€์ฒ ์ˆ˜ 25 false
console.log(
  person1['name'], // ์†์„ฑ๋ช…์„ string์œผ๋กœ
  person1['age'],
  person1['married'],
); // ๊น€์ฒ ์ˆ˜ 25 false

 

๐Ÿ’ก ์กด์žฌํ•˜์ง€ ์•Š๋Š” ํ‚ค๋กœ ์ ‘๊ทผ์‹œ undefined ๋ฐ˜ํ™˜

console.log(person1.birthdate); // undefined
console.log(person1['job']); // undefined

 

๐Ÿ’ก ํ‚ค in ๊ฐ์ฒด - ํŠน์ • ํ‚ค ํฌํ•จ ์—ฌ๋ถ€ ํ™•์ธ

console.log(
  'age' in person1,
  'job' in person1
); // true false

 

(2) ํ”„๋กœํผํ‹ฐ ์ˆ˜์ • ๋ฐ ์ถ”๊ฐ€

 

๐Ÿ’ก ํŠน์ • ํ”„๋กœํผํ‹ฐ์˜ ๊ฐ’ ๋ณ€๊ฒฝ

person1.age = 26;
person1['married'] = true

console.log(person1); // {name: '๊น€์ฒ ์ˆ˜', age: 26, married: true}

 

๐Ÿ’ก ์ƒˆ ํ”„๋กœํผํ‹ฐ ์ถ”๊ฐ€

person1.job = 'developer';
person1['bloodtype'] = 'AB'

console.log(person1); // {name: '๊น€์ฒ ์ˆ˜', age: 26, married: true, job: 'developer', bloodtype: 'AB'}

- const ์ž„์—๋„ ๊ทธ ๋‚ด์šฉ์€ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์Œ

 


2. ๋ฐฐ์—ด ๋ฏธ๋ฆฌ๋ณด๊ธฐ

const winners = [12, 592, 7, 48];
const weekdays = ['์›”', 'ํ™”', '์ˆ˜', '๋ชฉ', '๊ธˆ', 'ํ† ', '์ผ'];

// ์ž๋ฃŒํ˜•์— ๊ด€๊ณ„์—†์ด ํ•œ ๋ฐฐ์—ด์— ๋„ฃ์„ ์ˆ˜ ์žˆ์Œ
const randoms = ['ํ™๊ธธ๋™', -24, true, null, undefined];

console.log(typeof winners); // object
console.log(winners, weekdays, randoms);
// [12, 592, 7, 48] 
// ['์›”', 'ํ™”', '์ˆ˜', '๋ชฉ', '๊ธˆ', 'ํ† ', '์ผ'] 
// ['ํ™๊ธธ๋™', -24, true, null, undefined]

 

(1) ๊ฐ’๊ณผ ๊ธธ์ด ์ ‘๊ทผ

 

๐Ÿ’ก ํŠน์ • ์ˆœ์„œ์˜ ๊ฐ’์— ์ ‘๊ทผํ•˜๋Š” ๋ฒ• (0๋ถ€ํ„ฐ ์‹œ์ž‘)

console.log(winners[0], weekdays[6], randoms[3]); // 12 '์ผ' null

 

๐Ÿ’ก ๋ฐฐ์—ด์˜ ๊ธธ์ด(์š”์†Œ์˜ ๊ฐฏ์ˆ˜)๋ฅผ ์–ป๋Š” ๋ฒ•

console.log(winners.length, weekdays.length, randoms.length); // 4 7 5
console.log(winners['length'], weekdays['length'], randoms['length']); // 4 7 5

 

๐Ÿ’ก ๋งˆ์ง€๋ง‰ ์š”์†Œ ์–ป๊ธฐ

console.log(winners[winners.length - 1]); // 48

 

(2) ์ˆ˜์ • ๋ฐ ์ถ”๊ฐ€

 

๐Ÿ’ก ํŠน์ • ์œ„์น˜์˜ ๊ฐ’ ์ˆ˜์ •

const numbers = [1, 2, 3];

// ํŠน์ • ์œ„์น˜์˜ ๊ฐ’ ์ˆ˜์ •
numbers[2] = 5;

console.log(numbers); // [1, 2, 5]

 

๐Ÿ’ก ๋ฐฐ์—ด์˜ ๋งจ ๋์— ๊ฐ’ ์ถ”๊ฐ€ : push()

// ๋งจ ๋์— ๊ฐ’ ์ถ”๊ฐ€
numbers.push(10);

console.log(numbers); // [1, 2, 5, 10]

- const ์ž„์—๋„ ๊ทธ ๋‚ด์šฉ์„ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์Œ

 

๐Ÿ’ก ๋ฐฐ์—ด์˜ ๋ฒ”์ฃผ ๋„ˆ๋จธ๋กœ ์ ‘๊ทผ์‹œ undefined ๋ฐ˜ํ™˜

const winners = [12, 592, 7, 48];
console.log(winners[winners.length]); // undefined

โญ๏ธ ์ค‘์ฒฉ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

const person2 = {
  name: '๊น€๋‹ฌ์ˆœ',
  age: 23,
  languages: ['Korean', 'English', 'French'],
  education: {
    school: 'ํ•œ๊ตญ๋Œ€',
    major: ['์ปดํ“จํ„ฐ๊ณตํ•™', '์ „์ž๊ณตํ•™'],
    graduated: true,
  }
};

console.log(person2.languages[2]); // French
console.log(person2.education.graduated); // true
const groups = [[1, 2], [3, 4, 5], [6, 7, 8, 9]];

const weapons = [
  { name: '๋กฑ์†Œ๋“œ', damage: 30, design: ['ํ™”๋ฃก๊ฒ€', '๋‡Œ์‹ ๊ฒ€'] },
  { name: 'ํ™œ', damage: 12 },
  { name: '์›Œํ•ด๋จธ', damage: 48 },
];

console.log(groups[1][2]); // 5
console.log(weapons[2].damage); // 48
console.log(weapons[0].design[0]); // ํ™”๋ฃก๊ฒ€

 

 

์ •๋ฆฌ์ฐธ์กฐ

https://www.yalco.kr/@javascript/2-9/

 

๊ฐ์ฒด์™€ ๋ฐฐ์—ด ๋ฏธ๋ฆฌ๋ณด๊ธฐ

์–ด๋ ค์šด ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ฐœ๋…๋“ค์„ ์‰ฝ๊ฒŒ ์„ค๋ช…ํ•ด์ฃผ๋Š” ์œ ํŠœ๋ธŒ ์ฑ„๋„ '์–„ํŒํ•œ ์ฝ”๋”ฉ์‚ฌ์ „'. ์˜์ƒ์—์„œ ๋‹ค ์•Œ๋ ค์ฃผ์ง€ ๋ชปํ•œ ์ •๋ณด๋“ค์ด๋‚˜ ์ž์ฃผ ๋ฌป๋Š” ์งˆ๋ฌธ๋“ค์˜ ๋‹ต๋ณ€๋“ค, ์˜ˆ์ œ ์ฝ”๋“œ๋“ค์„ ์–„์ฝ”์—์„œ ํ™•์ธํ•˜์„ธ์š”!

www.yalco.kr