Data Structures: Objects and Arrays - Lee-hyuna/33-js-concepts-kr GitHub Wiki

Data Structures: Objects and Arrays

๋ฒˆ์—ญ : https://scotch.io/courses/10-need-to-know-javascript-concepts/data-structures-objects-and-arrays#toc-what-is-an-object-

JavaScript์—์„œ ๋Œ€๊ด„ํ˜ธ์™€ ์ค‘๊ด„ํ˜ธ๋กœ ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณธ ์ ์ด ์žˆ์Šต๋‹ˆ๊นŒ? ์ด๋“ค์€ ๊ฐ๊ฐ ๋ฐฐ์—ด๊ณผ ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค.

// arrays
[]

// objects
{}

JavaScript๋กœ ๊ฐ์ฒด์™€ ๋ฐฐ์—ด์„ ๋…ผ์˜ ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด JavaScript์—์„œ ๋ฐ์ดํ„ฐ ์œ ํ˜•์„ ์•Œ๊ณ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

  • String
  • Number
  • Boolean
  • Null
  • Undefined

๊ฐ์ฒด์™€ ๋ฐฐ์—ด์€ ๋‹ค๋ฅด์ง€ ์•Š์ง€๋งŒ ์ข€ ๋” ๋ณต์žกํ•œ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๊ฒƒ๋“ค์€ JavaScript์—์„œ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ ์œ ํ˜•์˜ ์ปจํ…Œ์ด๋„ˆ๋กœ ์‹œ๊ฐํ™” ๋  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋ผ๋Š” ์šฉ์–ด์ž…๋‹ˆ๋‹ค.

What is an Object?

JavaScript ๊ฐ์ฒด๋Š” ์•Œ๊ณ ์žˆ๋Š” ์ž์—ฐ ๊ฐ์ฒด์™€ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์ปต, ์ง‘, ์ž๋™์ฐจ ๋“ฑ

์ด๋“ค ๊ฐ์ฒด๊ฐ€ ํ• ์ˆ˜ ์žˆ๋Š” ๊ฒƒ๋“ค:

  • ๋ช‡๊ฐ€์ง€๋ฅผ ํฌํ•จ
  • ํŠน๋ณ„ํ•œ ํŠน์ง•์„ ์ง€๋‹˜
  • ๋‹ค๋ฅธ ๊ฐ์ฒด๋ฅผ ํฌํ•จํ•  ์ˆ˜ ์žˆ์Œ
  • ๋ช‡๊ฐ€์ง€ ๋ชฉ์ ์„ ์œ„ํ•ด ์‚ฌ์šฉํ•  ์ˆ˜์žˆ์Œ
  • ๊ทธ๋“ค์—๊ฒŒ ํ–‰ํ•ด์ง„ ๋ช‡ ๊ฐ€์ง€ ํ–‰๋™์„ ๋ฐ›์•„ ๋“ค์ผ ์ˆ˜์žˆ๋‹ค

์˜ˆ๋ฅผ ๋“ค์–ด, ์ปต์— ์ž‘์€ ์ปต์ด์žˆ์„ ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ปต์—์„œ ๋ฌผ์„ ๋งˆ์‹œ๊ณ  ์ปต์„ ๊นจ๊ณ  ์ž๋™์ฐจ๋ฅผ ์šด์ „ํ•˜๊ฑฐ๋‚˜ ์–ด๋ฆฐ์ด ๋˜๋Š” ์žฅ๋‚œ๊ฐ ์ž๋™์ฐจ๋ฅผ ์ž๋™์ฐจ์— ๋„ฃ๊ณ  ๋ชฉ๋ก์ด ๊ณ„์† ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

JavaScript ๊ฐ์ฒด๋Š” ๋‹ค๋ฅด์ง€ ์•Š์œผ๋ฉฐ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

const myObject = {};

์ค‘๊ด„ํ˜ธ๋Š” ๊ฐœ์ฒด๋ฅผ ๋‚˜ํƒ€๋‚ด๋ฉฐ ๊ฐœ์ฒด์˜ ์š”์†Œ๊ฐ€ ๊ฐœ์ฒด๋ฅผ ๊ตฌ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๊ฐ์ฒด ๊ฐ’์€ ํ‚ค : ๊ฐ’ ์Œ์œผ๋กœ ์ œ๊ณต๋˜๋ฉฐ ์ด๋Ÿฌํ•œ ๊ฐ’์€ ๊ฐ์ฒด์˜ ์†์„ฑ ๋˜๋Š” ๋ฐฉ๋ฒ• (ํ•จ์ˆ˜) ์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์†์„ฑ์€ ๊ฐ์ฒด์˜ ํŠน์ง• ๋˜๋Š” ์†์„ฑ ์ธ ๋ฐ˜๋ฉด, ๋ฐฉ๋ฒ•์€ ๊ฐ์ฒด์—์„œ ์ˆ˜ํ–‰ ํ•  ์ˆ˜์žˆ๋Š” ๊ธฐ๋Šฅ ๋˜๋Š” ๋™์ž‘์ž…๋‹ˆ๋‹ค.

์ฐธ๊ณ  : ๊ฐ์ฒด๊ฐ€ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ์ธ์‹ํ•˜๋ ค๋ฉด ๊ฐ์ฒด์—์„œ ๊ฐ์ฒด ๋ฉ”์†Œ๋“œ๋ฅผ ์ •์˜ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

๊ฐ์ฒด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‚˜ํƒ€๋‚ฉ๋‹ˆ๋‹ค.

const myObject = {
    myKey1: 'ObjectValue1',
    myKey2: 'ObjectValue2',
    mykeyN: 'ObjectValueN',
    objectMethod: function() {
      // Do something on this object
    }
};

๊ธฐ๋ณธ์ ์œผ๋กœ JavaScript์˜ ๊ฑฐ์˜ ๋ชจ๋“  ๊ฒƒ์ด Object์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ด์œ ๋กœ ๋งŽ์€ ์‚ฌ๋žŒ๋“ค์ด JavaScript๋ฅผ ๊ฐ์ฒด ์ง€ํ–ฅ ์–ธ์–ด๋กœ ๊ฐ„์ฃผํ•ฉ๋‹ˆ๋‹ค. ๋ธŒ๋ผ์šฐ์ €์—์„œ ์œˆ๋„์šฐ๊ฐ€ ๋กœ๋“œ๋˜๋ฉด Document ๊ฐœ์ฒด์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์„ฑ๋˜๊ณ  ๋ธŒ๋ผ์šฐ์ €์— ํ‘œ์‹œ๋œ ๋‹ค๋ฅธ ๋ชจ๋“  ๊ฒƒ์€ ๋ฌธ์„œ ๊ฐœ์ฒด์˜ ์ž์‹์ด๋ฉฐ ๋ฌธ์„œ ๋ฉ”์„œ๋“œ๋Š” ๋ถ€๋ชจ ๊ฐœ์ฒด๋ฅผ ์กฐ์ž‘ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

๊ฐ์ฒด๋ฅผ ์ค‘์ฒฉ ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

const myObject = {
  first:  { key:  'thing', key2: 2 },
  second: { key3: 'otherThing' }, 
  third:  { key4: 'my string' }
};

Everything is an Object

๋ชจ๋“  ๊ฒƒ์ด ๊ฐ์ฒด๋ผ๊ณ  ๋งํ•˜๋ฉด JavaScript๋Š” ๋ชจ๋“  ์œ ํ˜•์„ ํ•ด๋‹น ์œ ํ˜•์— ๋Œ€ํ•œ ๋ฉ”์†Œ๋“œ๋ฅผ ์ œ๊ณตํ•˜๋Š” ๊ฐ์ฒด๋กœ ๋ž˜ํ•‘ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ๋ช…ํ™•ํžˆํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ์‚ดํŽด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

// create a message with a string value
const myMessage = 'look at me go!';

// convert to uppercase
myMessage.toUpperCase();

์ด ๋ฌธ์ž์—ด์—์„œ ์–ด๋–ป๊ฒŒ .toUpperCase () ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœ ํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๊นŒ? ์šฐ๋ฆฌ๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ๊ฐ์ฒด์— ๋Œ€ํ•ด์„œ๋งŒ ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด์„ ๋งŒ๋“ค ๋•Œ JavaScript๋Š” ์‹ค์ œ๋กœ ํ•ด๋‹น ๋ฌธ์ž์—ด์„ string ์œ ํ˜•์˜ ๊ฐ์ฒด๋กœ ๋ž˜ํ•‘ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค

// simple way to create a string
const myMessage = 'look at me go!';

// what javascript sees
const myOtherMessage = String('look at me go!');

myMessage == myOtherMessage; // true

์ด์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ JavaScript ๊ฐ์ฒด์— ๋Œ€ํ•œ ๋น„๋””์˜ค๋ฅผ ํ™•์ธํ•˜์‹ญ์‹œ์˜ค. [์›น ๊ฐœ๋ฐœ์„์œ„ํ•œ JavaScript ์‹œ์ž‘ : ๊ฐ์ฒด] (https://scotch.io/courses/getting-started-with-javascript/objects)

Creating Objects

๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ช‡ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค :

Using Object Initializers

์ด๋Š” ํ‚ค : ๊ฐ’ ์Œ๊ณผ ๋ฉ”์†Œ๋“œ๋ฅผ ์ค‘๊ด„ํ˜ธ ์•ˆ์— ๋„ฃ์–ด์„œ ์œ„์— ํ‘œ์‹œ๋œ๋Œ€๋กœ ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์„ ๋ฌธ์ž ํ‘œ๊ธฐ๋ฒ•์ด๋ผ๊ณ ๋„ํ•ฉ๋‹ˆ๋‹ค.

const obj = {
    firstName: 'William',
    lastName:  'Imoh',
    married:   true,
    greet:     function() {
      alert("Hello everyone, i'm " + this.firstName);
    }
};

์œ„์˜ ์ฝ”๋“œ์—์„œ ๋‹ค์Œ์„ ํ˜ธ์ถœํ•˜์—ฌ ์ด๋Ÿฌํ•œ ์†์„ฑ๊ณผ ๋ฉ”์„œ๋“œ์— ์•ก์„ธ์Šค ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

obj.firstName;   // William
obj.lastName;    // Imoh
obj.greet();     // Will alert, Hello everyone, i'm William 

์™œ ์šฐ๋ฆฌ๊ฐ€ obj.firstName ๋Œ€์‹ ์— ์ด๊ฒƒ์„ this ๋ฅผ ์‚ฌ์šฉํ–ˆ๋Š”์ง€ ๊ถ๊ธˆ ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ž, this ๋Š” ๊ทธ๊ฒƒ์ด ํ˜ธ์ถœ๋˜๋Š” ๋ถ€๋ชจ ๊ฐ์ฒด๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋Œ€๋ช…์‚ฌ์™€ ๊ฐ™์€ ์—ญํ• ์„ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ ๊ฐ•์˜์—์„œ ์ด์— ๋Œ€ํ•ด ๋” ๋ฐฐ์šธ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

Using Object Constructor

new ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ์ฒด ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

const house     = new Object();

house.color     = 'red';
house.houseType = 'bungalow';
house.location  = 'Lagos';

์šฐ๋ฆฌ๊ฐ€ ํ•œ ๊ฒƒ์€ ๊ฐ์ฒด ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๋ณ€์ˆ˜ ํ•˜์šฐ์Šค์— ํ• ๋‹น ํ•œ ๋‹ค์Œ ์†์„ฑ๊ณผ ๊ฐœ๋ณ„ ๊ฐ’์„ ํŽธ์ง‘ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

๋˜ํ•œ ํ•จ์ˆ˜ ์ƒ์„ฑ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ์ฒด ๋˜๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์œ ์‚ฌํ•œ ๊ฐ์ฒด๋ฅผ๋ณด๋‹ค ์‰ฝ๊ฒŒ โ€‹โ€‹๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

function University(name, location, size) {
  this.name     = name;
  this.location = location;
  this.size     = size;
}

const theUniversity = new University('caritas', 'enugu', 20000);

theUniversity.name;     // caritas
theUniversity.size;     // 20000

์—ฌ๊ธฐ์„œ ํ•จ์ˆ˜๋Š” ์ธ์ˆ˜๋กœ ๊ตฌ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํ•จ์ˆ˜ ์ด๋ฆ„์€ ๋Œ€๋ฌธ์ž๋กœ๋˜์–ด ์žˆ์œผ๋ฉฐ ์ƒ์„ฑ์ž ํ•จ์ˆ˜์— ๋Œ€ํ•œ ๊ทœ์น™์ž…๋‹ˆ๋‹ค. ์ด ํ•จ์ˆ˜์—๋Š” ์ธ์ˆ˜๊ฐ€ ์ „๋‹ฌ๋˜๋ฉฐ์ด ์ธ์ˆ˜๋Š” this.property ์— ์˜ํ•ด ์ •์˜ ๋œ ๊ฐ์ฒด ์†์„ฑ์˜ ๊ธฐ๋ณธ ๊ฐ’์ž…๋‹ˆ๋‹ค.

์ƒˆ ํ‚ค์›Œ๋“œ๋Š” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  ํ•จ์ˆ˜์— ์ •์˜ ๋œ ๋งค๊ฐœ ๋ณ€์ˆ˜์™€ ์ผ์น˜ํ•˜๋Š” ๋งค๊ฐœ ๋ณ€์ˆ˜๋ฅผ ์ „๋‹ฌํ•˜์—ฌ ์ƒˆ ํ‚ค์›Œ๋“œ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์„ ์‹คํ–‰ํ•˜์‹ญ์‹œ์˜ค.

console.log(theUniversity)

๊ทธ๋ฆฌ๊ณ  ์šฐ๋ฆฌ๋Š” ์ด๊ฒƒ์„ ์–ป์Šต๋‹ˆ๋‹ค :

University {
  name:     'caritas',
  location: 'enugu',
  size:     '20000'
}

Accessing an Object

๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์œผ๋กœ ๊ฐ์ฒด์˜ ์†์„ฑ์— ์•ก์„ธ์Šค ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Dot notation

์  ํ‘œ๊ธฐ๋ฒ•์—์„œ ๊ฐ์ฒด์˜ ์†์„ฑ ๊ฐ’์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์•ก์„ธ์Šค๋ฉ๋‹ˆ๋‹ค.

const objectName = {
  objectProp: 'my super duper value',
};

objectName.objectProp; // my super duper value

Square Bracket Notation

๋Œ€๊ด„ํ˜ธ ํ‘œ๊ธฐ๋ฒ•์€ ์  ํ‘œ๊ธฐ๋ฒ•์˜ ๋Œ€์•ˆ์ด๋ฉฐ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑ๋ฉ๋‹ˆ๋‹ค.

const objectName = {
  objectProp: 'super cool yo!',
};

objectName['objectProp']; // super cool yo!

์†์„ฑ ์ด๋ฆ„์ด ๋Œ€์‹œ ๋‚˜ ์ˆซ์ž์™€ ๊ฐ™์€ ํŠน์ˆ˜ ๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•˜๊ฑฐ๋‚˜ ๋ณ€์ˆ˜๋ฅผ ํ‚ค๋กœ ์‚ฌ์šฉํ•˜๋ ค๋Š” ๊ฒฝ์šฐ ๋Œ€๊ด„ํ˜ธ ํ‘œ๊ธฐ๋ฒ• ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ํ˜„๋Œ€์ ์ธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๊ฐ€ ์‹œ์ž‘๋œ ์ด๋ž˜๋กœ for-loop๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์„ธํŠธ๋ฅผ ์ˆœํšŒํ•˜๋Š” ๊ฒƒ์€ ๋งŽ์€ ๋น„์šฉ์ด ๋“ค์ง€ ์•Š์•˜์œผ๋ฉฐ Objects์—๋Š” ๊ฐ์ฒด ์†์„ฑ์„ ์ˆœํšŒํ•˜๊ฑฐ๋‚˜ ๋ฐ˜๋ณตํ•˜๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค.

  • forโ€ฆin loop: ์ด๊ฒƒ์€ ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์ธ ๊ฒƒ์œผ๋กœ ๊ฐ์ฒด์˜ ์—ด๊ฑฐ ๊ฐ€๋Šฅํ•œ ๊ฐ ์†์„ฑ๊ณผ ํ”„๋กœํ†  ํƒ€์ž… ์ฒด์ธ์„ ๋ฐ˜๋ณตํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
  • Object.keys(o): object.keys (o) ๋ฉ”์†Œ๋“œ๋Š” ๊ฐ์ฒด o๋ฅผ ์ˆœํšŒํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋ฉฐ ์—ด๊ฑฐ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ์†์„ฑ ์ด๋ฆ„์ด ํฌํ•จ ๋œ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  • Object.getOwnPropertyNames(o): ๋œ ์•Œ๋ ค์ง„ ๋ฐฉ๋ฒ•์€ ๊ฐ์ฒด o๋ฅผ ๋ฐ˜๋ณตํ•˜๊ณ  ์—ด๊ฑฐ ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด์˜ ๋ชจ๋“  ์ž์—ฐ ์†์„ฑ ์ด๋ฆ„ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

Arrays

๋ฐฐ์—ด์€ ํŠน๋ณ„ํ•œ ์ข…๋ฅ˜์˜ JavaScript ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค.

const a = [];

typeof a; // returns object

๋ฐฐ์—ด์€ ์—ฌ๋Ÿฌ ๋ณ€์ˆ˜, ์ฆ‰ 300 ๋งˆ๋ฆฌ์˜ ๋‹ญ์„ ์ทจํ•˜๋Š” ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค! ๊ฐ์ฒด ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ์™€ ๋‹ฌ๋ฆฌ ํ‚ค : ๊ฐ’ ์Œ์€ ์—†์ง€๋งŒ ๋‹จ์ˆ˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐฐ์—ด์„ ์–ด๋–ป๊ฒŒ ์‹๋ณ„ํ•ฉ๋‹ˆ๊นŒ? ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณด์ž…๋‹ˆ๋‹ค :

const myArray = ['hi', 'i', 'am', 'chief'];

๋ฐฐ์—ด์˜ ๋ฐ์ดํ„ฐ๋Š” ๋Œ€๊ด„ํ˜ธ๋กœ ๋ฌถ๊ณ  ์›ํ•˜๋Š”๋งŒํผ์˜ ๋ณ€์ˆ˜์™€ ๋‹ค๋ฅธ ๋ฐฐ์—ด์„ ํฌํ•จ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐฐ์—ด์€ ๊ฐ์ฒด์ด๋ฉฐ ๊ฐ์ฒด๋„ ํฌํ•จ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

const myArray = [
  { name: 'chris', location: 'racoon city' }
];

Creating Arrays

๋ฐฐ์—ด์€ ๋‘ ๊ฐ€์ง€ ๋ฐฉ์‹์œผ๋กœ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.

Array Literal

๋ฐฐ์—ด ๋ฆฌํ„ฐ๋Ÿด์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋ฐฐ์—ด์„ ๋งŒ๋“œ๋Š” ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์ธ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๋ฐฐ์—ด ๊ฐ’์€ ๋Œ€๊ด„ํ˜ธ๋กœ ๋ฌถ์Šต๋‹ˆ๋‹ค.

const arr = [];
const bar = ['drinks', 'music', 'dance', 'beer', 'more beer'];

// an array of objects
const coocoo = [
  { key:  'thing', key2: 2 },
  { key3: 'otherThing' }, 
  { key4: 'my string' }
];

Array Constructor

์ด๊ฒƒ์€ ์ƒˆ๋กœ์šด ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐฐ์—ด์„ ๋งŒ๋“œ๋Š” ๋˜ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์Šคํฌ๋ฆฝํŠธ์—์„œ ์˜ˆ๊ธฐ์น˜ ์•Š์€ ๋™์ž‘์„ ์ผ์œผํ‚ฌ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๋งŽ์€ ์‚ฌ๋žŒ๋“ค์ด ๋‚˜์œ ์Šต๊ด€์œผ๋กœ ๊ฐ„์ฃผํ•ฉ๋‹ˆ๋‹ค. ๋ฐฐ์—ด ๊ฐ’์€ ์ƒ์„ฑ์ž์— ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค.

const bar = new Array('beer', 'music', 'beer', 'more beer');

console.log(bar);  // ['beer', 'music', 'beer', 'more beer']

๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฝ์šฐ :

const fig = new Array(10, 20);

console.log(fig);   // [10, 20]

๊ทธ๋ฆผ์ด ์ œ๊ฑฐ๋˜๋ฉด ์–ด๋–ป๊ฒŒ ๋˜๋‚˜์š”? ์šฐ๋ฆฌ๋Š” :

const fig = new Array(10);

console.log(fig);   // returns 10 empty arrays!

์šฐ๋ฆฌ๋Š” 10 ๊ฐœ์˜ ๋นˆ ๋ฐฐ์—ด์„ ์–ป์Šต๋‹ˆ๋‹ค! ๋ฐฐ์—ด ๋ฆฌํ„ฐ๋Ÿด []์„ ์‚ฌ์šฉํ•˜๋ฉด ์ฝ๊ธฐ๊ฐ€ ์‰ฝ๊ณ  ์“ฐ๊ธฐ๊ฐ€ ์ ์œผ๋ฉฐ ์˜ˆ์ƒ๋Œ€๋กœ ์ž‘๋™ํ•˜๋ฏ€๋กœ ํ•ญ์ƒ ๊ถŒ์žฅ๋ฉ๋‹ˆ๋‹ค.

Accessing Arrays

๊ฐ์ฒด์™€ ๋‹ฌ๋ฆฌ ๋ฐฐ์—ด ๊ฐ’์€ 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” ์ƒ‰์ธ โ€‹โ€‹์œ„์น˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์•ก์„ธ์Šคํ•ฉ๋‹ˆ๋‹ค (๋ฐฐ์—ด์€ 0์œผ๋กœ ์ƒ‰์ธํ™” ๋จ). ๊ฐ์ฒด๋Š” ๋ฌธ์ž ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐ˜๋ฉด ๋ฐฐ์—ด์€ ์ˆซ์ž ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ฐฐ์—ด์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์•ก์„ธ์Šค๋ฉ๋‹ˆ๋‹ค :

const myArr = [10, 40, 50, 70, 20, 2, 100];

myArr[0] // 10
myArr[3] // 70
myArr[7] // 100

arr.firstNumber ์™€ ๊ฐ™์€ ๋ฐฐ์—ด์—์„œ ๋ฌธ์ž ์ƒ‰์ธ์„ ์‚ฌ์šฉํ•˜๋ ค๊ณ ํ•˜๋ฉด ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

Array Methods

๊ฐ์ฒด์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ฐฐ์—ด์—๋„ ์กฐ์ž‘์— ์‚ฌ์šฉํ•  ์ˆ˜์žˆ๋Š” ๋ฉ”์†Œ๋“œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ฉ”์†Œ๋“œ๋Š” ๋‚ด์žฅ๋˜๊ฑฐ๋‚˜ ๊ตฌ์„ฑ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ ๋ฐฐ์—ด ๋ฐฉ๋ฒ•์ด ์žˆ์œผ๋ฉฐ ์—ฌ๊ธฐ ์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

const fig = [2, 5, 10, 30, 20];

arr.length;   // returns the length of the array = 5

for ๋ฃจํ”„๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐฐ์—ด์„ ๋ฐ˜๋ณตํ•˜๋Š” ๊ฒƒ์€ ๋งค์šฐ ์›ํ™œํ•ฉ๋‹ˆ๋‹ค. ์œ„์˜ ์˜ˆ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด

const fig = [2, 5, 10, 30, 20];

for (let i = 0; i < fig.length; i++) {
  console.log(fig[i]);
}

// 2
// 5
// 10
// 30
// 20

๋‹ค๋ฅธ ๋ฐฐ์—ด ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • pop(): ๋ฐฐ์—ด์—์„œ ๋งˆ์ง€๋ง‰ ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ํ•ด๋‹น ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜
  • slice(): ๋ฐฐ์—ด ๋ถ€๋ถ„์˜ ์–•์€ ๋ณต์‚ฌ๋ณธ์„ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜
  • shift(): ๋ฐฐ์—ด์—์„œ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ํ•ด๋‹น ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜
  • unshift(): ๋ฐฐ์—ด์˜ ์‹œ์ž‘ ๋ถ€๋ถ„์— ํ•˜๋‚˜ ์ด์ƒ์˜ ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ๋ฐฐ์—ด์˜ ์ƒˆ ๊ธธ์ด๋ฅผ ๋ฐ˜ํ™˜

๋ฐฐ์—ด ์ž‘์—…์„ ํ•  ๋•Œ๋Š” ํ•ญ์ƒ ๋‚ด์žฅ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ดํŽด๋ณด์‹ญ์‹œ์˜ค. ์ผ๋ฐ˜์ ์œผ๋กœ ํ•„์š”ํ•œ ๊ฒƒ์„ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋ฐฐ์—ด ๋ฉ”์„œ๋“œ๋Š” ** mutator ๋ฉ”์„œ๋“œ **, ** accessor ๋ฉ”์„œ๋“œ ** ๋ฐ ** iteration ๋ฉ”์„œ๋“œ **๋กœ ๊ทธ๋ฃนํ™” ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฎค ํ…Œ์ดํ„ฐ ๋ฉ”์†Œ๋“œ๋Š” ์‹ค์ œ๋กœ ๋ฐฐ์—ด์„ ๋ณ€๊ฒฝํ•˜๊ฑฐ๋‚˜ ์ˆ˜์ •ํ•˜๋Š” ๋ฉ”์†Œ๋“œ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด array.push () ๋ฉ”์†Œ๋“œ๋Š” ์ง€์ •๋œ ์š”์†Œ๋ฅผ ๋ฐฐ์—ด์— ์ถ”๊ฐ€ํ•˜๊ณ  ์ˆ˜์ • ๋œ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

const fig = [10, 20, 30, 40];

fig.push(80);
console.log(fig);    // [10, 20, 30, 40, 80]

์ ‘๊ทผ ์ž ๋ฉ”์†Œ๋“œ๋Š” ๋ฐฐ์—ด์„ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š๊ณ  ๋ฉ”์†Œ๋“œ์˜ ํšจ๊ณผ์— ๋”ฐ๋ผ ๋ฐฐ์—ด์˜ ์ด๋ฏธ์ง€๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๋ฉ”์†Œ๋“œ์ž…๋‹ˆ๋‹ค ์˜ˆ : array.slice ()

const fig = [10, 20, 30, 40];
const b   = fig.slice(1);

console.log(b);      // [20, 30, 40]
console.log(fig);    // [10, 20, 30, 40]

์ด๋ฆ„์—์„œ ์•Œ ์ˆ˜ ์žˆ๋“ฏ์ด ๋ฐ˜๋ณต ๋ฉ”์†Œ๋“œ๋Š” ๋ฐฐ์—ด์˜ ๊ธธ์ด๋ฅผ ์ƒ˜ํ”Œ๋งํ•˜๊ณ  ๋ฉ”์†Œ๋“œ์— ์ •์˜ ๋œ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋กœ ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ๋ฅผ ํ‰๊ฐ€ํ•˜๋Š” ๋™์•ˆ ๋ฐฐ์—ด์„ ๋ฐ˜๋ณตํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—๋Š” .forEach ()๋ฐ.map () ๋ฉ”์†Œ๋“œ๊ฐ€ ํฌํ•จ๋ฉ๋‹ˆ๋‹ค.

const fig = [10, 20, 30, 40];

const newFig = fig.map(function(val) { //calls a function on each element of the array
  return val + 10;
});

console.log(newFig) // [20,30,40,50]

๋งˆ์ง€๋ง‰์œผ๋กœ ๋ฐฐ์—ด์—๋Š” ๊ธธ์ด๋ฅผ ํฌํ•จํ•˜๋Š” ์†์„ฑ์ด ์žˆ์œผ๋ฉฐ ๋ฐฐ์—ด์—๋Š” ๊ฐ์ฒด๊ฐ€ ๋งŒ๋“ค์–ด ์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Conclusion

์šฐ๋ฆฌ๋Š” ๊ฐ์ฒด๊ฐ€ ๋ฌด์—‡์ธ์ง€, ์–ด๋–ป๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š”์ง€, ์†์„ฑ๊ณผ ๊ฐ’์„ ์ฒ˜๋ฆฌํ•˜์—ฌ ํŠน์ˆ˜ํ•˜๊ฒŒ ๋งŒ๋“œ๋Š” ์ด์œ ๋ฅผ ๋ช…ํ™•ํ•˜๊ฒŒ ์ดํ•ดํ•ฉ๋‹ˆ๋‹ค. ๊ฐ์ฒด์—๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์•„ํ‚คํ…์ฒ˜์—์„œ ์„œ๋ฒ„ ์‘๋‹ต์— ์ด๋ฅด๊ธฐ๊นŒ์ง€ ๋‹ค์–‘ํ•œ ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

๋ฐ˜๋ฉด์— ๋ฐฐ์—ด์€ ์ปจํ…Œ์ด๋„ˆ์™€ ๊ฐ™์€ ๋ฐ์ดํ„ฐ ๋ชจ์Œ์ž…๋‹ˆ๋‹ค. ๊ทธ๊ฒƒ์˜ ์‚ฌ์šฉ ๋˜ํ•œ ๊ด‘๋Œ€ํ•˜๋‹ค. ๋ฐฐ์—ด๊ณผ ๊ฐ์ฒด ๋ชจ๋‘์— ์‚ฌ์šฉํ•  ์ˆ˜์žˆ๋Š” ์ˆ˜๋งŽ์€ ๋ฐฉ๋ฒ•์œผ๋กœ ์ž์œ ๋กญ๊ฒŒ ๋†€์•„๋ณด์‹ญ์‹œ์˜ค. ํ•ญ์ƒ ์†์— ๋‹ฟ๋Š”๋‹ค๋Š” ๊ฒƒ์„ ํ•ญ์ƒ ๊ธฐ์–ตํ•˜์‹ญ์‹œ์˜ค. ์—ฌ๋Ÿฌ ๋ฒˆ ๊ฐ์ฒด์˜ ๋ฐฐ์—ด์„ ๋งŒ๋“ค ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์‚ฌ์šฉ์ž ๊ฐœ์ฒด ๋ชฉ๋ก์ด์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.