TIL.12 - Object๊ฐ์ฒด

quokkaยท2021๋…„ 10์›” 11์ผ
0

javascriptย 

๋ชฉ๋ก ๋ณด๊ธฐ
12/13
post-thumbnail

๐ŸŒˆ Object (๊ฐ์ฒด)

๐Ÿ‘‡ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์–ธ์–ด๋กœ ์ด ๋ฐ์ดํ„ฐ๋ฅผ ๊ตฌํ˜„ ํ•˜๋ ค๋ฉด, ์•„๋ž˜์™€ ๊ฐ™์ด ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

let plan1Name = "Basic";
let plan1Price = 3.99;
let plan1Space = 100;
let plan1Data = 1000;
let plan1Pages = 10;
let plan2Name = "Professional";
let plan2Price = 5.99;
let plan2Space = 500;
let plan2Data = 5000;
let plan2Pages = 50;
let plan3Name = "Ultimate";
let plan3Price = 9.99;
let plan3Space = 2000;
let plan3Data = 20000;
let plan3Pages = 500;

์ด ์ค‘์—์„œ ํ•˜๋‚˜์˜ plan์„ ์„ ํƒํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ดย alertย ๋ฅผ ๋„์›Œ์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.

alert(plan2Name + "ํ”Œ๋žœ ๊ฐ€๊ฒฉ์€ ํ•œ ๋‹ฌ์— " + plan2Price + "๋‹ฌ๋Ÿฌ์ž…๋‹ˆ๋‹ค.")

๋งŒ์•ฝ ์ž์„ธํ•œ ์„ค๋ช…๊นŒ์ง€ ํ•ด์ฃผ๋ ค๋ฉด,

alert("์šฉ๋Ÿ‰์€ " + plan2Space + "GB์ด๊ณ , ...");

์ด๋Ÿฐ์‹์œผ๋กœ ํ•ด๋‹น ํ”Œ๋žœ์— ๋Œ€ํ•œ ๋ณ€์ˆ˜ ๋‚˜์—ด์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด ์—ญ์‹œ ๋ฐ์ดํ„ฐ๊ฐ€ ๋Š˜์–ด๋‚˜๋ฉด ๋Š˜์–ด๋‚ ์ˆ˜๋ก ์ฝ”๋“œ๋„ ๊ธธ์–ด์ง€๊ณ  ๊ด€๋ฆฌํ•˜๊ธฐ๋„ ํž˜๋“ค์–ด์ง‘๋‹ˆ๋‹ค.
๐Ÿ‘‰ ์ด๋Ÿด ๋•Œ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ๊ฒŒ Object ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค.๐Ÿ‘‡

๐Ÿ‘‰ plan1,ย plan2,ย plan3ย ๋ผ๋Š” object๊ฐ€ ์žˆ๋‹ค.
๐Ÿ‘‰ ๊ฐ object๋Š”ย name,ย price,ย space,ย transfer,ย pagesย ๋ผ๋Š” 5๊ฐœ์˜ property๊ฐ€ ์žˆ๋‹ค.
๐Ÿ‘‰ nameย ์ด๋ผ๋Š” property์˜ ๋ฐ์ดํ„ฐ๊ฐ’์€ Stringํ˜•์ด๊ณ , ๋‚˜๋จธ์ง€ 4๊ฐœ์˜ property๋Š” Number์ด๋‹ค.

๐Ÿ’Ž ๊ฐ์ฒด ์„ ์–ธ, ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

var new = {
	(property)name: (property๊ฐ’),
	(property)age: (property๊ฐ’)
};

๐Ÿ‘‰ ๊ฐ์ฒด๋Š” {} ์ค‘๊ด„ํ˜ธ๋กœ ๊ฐ์‹ธ์ ธ ์žˆ๋‹ค.
๐Ÿ‘‰ ์ฝœ๋ก ( : )์œผ๋กœ ์ด๋ฆ„๊ณผ ๊ฐ’๋“ค์ด ๊ตฌ๋ถ„๋œ๋‹ค.
๐Ÿ‘‰ ์‰ผํ‘œ๋กœ ๋ถ„๋ฆฌ๋œ ๋ชฉ๋ก์˜ ํ˜•ํƒœ์ด๋‹ค.
โœ๏ธ ๊ฐ์ฒด๋Š” ์ด๋ฆ„๊ณผ ๊ฐ’์œผ๋กœ ๊ตฌ์„ฑ๋œ property๋“ค์˜ ์ง‘ํ•ฉ์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค.

person ์ด๋ผ๋Š” ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด ๋ณด์ž๋ฉด ๐Ÿ‘‡๐Ÿ‘‡

let person = {
  name: "hoo",
  age: 25,
  hobby: "music"
};

โœ๏ธ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค ๋•Œ ๊ทœ์น™
๐Ÿ‘‰ property์˜ ์ด๋ฆ„์€ ์ค‘๋ณต๋  ์ˆ˜ ์—†๋‹ค.
๐Ÿ‘‰ property ์ด๋ฆ„๊ณผ property ๊ฐ’ ์‚ฌ์ด์— : ์œผ๋กœ ๊ตฌ๋ถ„ํ•œ๋‹ค.
๐Ÿ‘‰ property๋ฅผ ์ถ”๊ฐ€ํ•  ๋•Œ๋Š”ย ,ย (์‰ผํ‘œ)๋ฅผ ๋ถ™์—ฌ์ค€๋‹ค.
๐Ÿ‘‰ property ๊ฐ’์—๋Š” ์–ด๋Š type์ด๋‚˜ ๊ฐ€๋Šฅํ•˜๋‹ค. (string, number, array, object, function..)

๐Ÿ’Ž ๊ฐ์ฒด์— ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ๋ฒ•

1. ๊ฐ์ฒด์ด๋ฆ„.ํ”„๋กœํผํ„ฐ์ด๋ฆ„
2. ๊ฐ์ฒด์ด๋ฆ„["ํ”„๋กœํผํ‹ฐ์ด๋ฆ„"]

๐Ÿ‘‰ ๋งˆ์นจํ‘œ . ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋งˆ์นจํ‘œ ์™ผ์ชฝ์—๋Š” ๊ฐ์ฒด๋ช…์„, ์˜ค๋ฅธ์ชฝ์—๋Š” ํ”„๋กœํผํ‹ฐ ๋ช…์„ ์ž‘์„ฑํ•œ๋‹ค.
๐Ÿ‘‰ ๋Œ€๊ณผ๋กœ [] ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋Œ€๊ด„ํ˜ธ ์™ผ์ชฝ์—๋Š” ๊ฐ์ฒด๋ช…์„ ์˜ค๋ฅธ์ชฝ์—๋Š” ํ”„๋กœํผํ‹ฐ ๋ช…์„ ์Œ๋”ฐ์˜ดํ‘œ์™€ ํ•จ๊ป˜ ๋Œ€๊ด„ํ˜ธ ์•ˆ์—๋‹ค ์ž‘์„ฑํ•œ๋‹ค.

let person = {
  name: "hoo",
  age: 25,
  hobby: "music"
};
console.log(person.name);
console.log(person["age"];

๐Ÿ”ฅ ๋Œ€๊ด„ํ˜ธ ์•ˆ์—๋Š” ๋ณ€์ˆ˜๊ฐ€ ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

let myObj = {
  property1: "hello",
  property2: [1,2,3,4,5],
  property3: {
    childproperty: "haha"
  }
};

๐Ÿ‘‡ ํ”„๋กœํผํ‹ฐ ์ˆ˜์ •

let name = "property1";
myObj[name] = ["hi", "hello"]; 
console.log(myObj);

โœ๏ธ ๊ฐ์ฒด์˜ property1์ด ๋ฐฐ์—ด๋กœ ๋ฐ”๋€Œ์—ˆ๊ณ , ์ด ๋ฐฐ์—ด์˜ ์ฒซ๋ฒˆ์งธ ์š”์†Œ์— ์ ‘๊ทผํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ๐Ÿ‘‡
๐Ÿ‘‰ console.log(myObj.property1[0]);
๐Ÿ”ฅ myObj.property1 ๋Š” ["hi", "hello"] ์ด๊ณ , index 0์— ์ ‘๊ทผํ•˜์˜€์œผ๋ฏ€๋กœ "hi" ๊ฐ€ ๋‚˜์˜ฌ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

๐Ÿ’Ž ๊ฐ์ฒด์— property ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•

myObj.property3.siblingproperty = [3, 6, 9];
console.log(myObj);

๐Ÿ’Ž ๊ฐ์ฒด์˜ ๊ฐ์ฒด์˜ ๋ฐฐ์—ด์˜ ๊ฐ์ฒด

let objData = {
  name: 50,
  address: {
    email: "gaebal@gmail.com",
    home: "์œ„์›Œํฌ ์„ ๋ฆ‰2ํ˜ธ์ "
  },
  books: {
    year: [2019, 2018, 2006],
    info: [{
      name: "JS Guide",
      price: 9000
    }, {
      name: "HTML Guide",
      price: 19000,
      author: "Kim, gae bal"
    }]
  }
};

๐Ÿ‘‰ objDataย ๋ผ๋Š” ๊ฐ์ฒด์•ˆ์—ย nameย ,ย addressย ,ย booksย ๋ผ๋Š” property๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
booksย ์— ๋˜ ๊ฐ์ฒด๊ฐ€ ํ• ๋‹น ๋˜์—ˆ๋„ค์š”.
๐Ÿ‘‰ "HTML Guide" ์— ์ ‘๊ทผํ•˜๊ณ  ์‹ถ์„ ๋•Œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๐Ÿ‘‡

let bookName = objData.books.info[1].name;
console.log(bookName);
let bookName = objData.book.info[1].name;
console.log(bookName);

๐Ÿ‘‰ objData.bookย ์€ย objDataย ๊ฐ์ฒด์˜ย bookย ์ด๋ผ๋Š” property๋ช…์˜ property๊ฐ’์„ ๊ฐ€์ ธ์™€์•ผ ํ•˜๋Š”๋ฐ,
๐Ÿ”ฅํ•ด๋‹น property๋ช…์€ ์กด์žฌํ•˜์ง€ ์•Š๊ณ , ๊ฐ’ ๋˜ํ•œ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๐Ÿ‘‰ย undefinedย ๋ผ๊ณ  ๋‚˜์˜ต๋‹ˆ๋‹ค.

profile
๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป ๋งค์ผ๋งค์ผ์ด ๊ธฐ๋Œ€๋˜๋Š” ๊ฐœ๋ฐœ์ž ^^

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