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

[ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ(JavaScript) ] ๊ฐ์ฒด ๋ฉ”์†Œ๋“œ(Object Methods)์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž.

by YWTechIT 2021. 8. 1.
728x90

๐Ÿ“ ๊ฐ์ฒด ๋ฉ”์†Œ๋“œ(Object Methods)์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž.

๊ธฐ๋ณธ์ ์œผ๋กœ ์›์‹œ ํƒ€์ž…(Primitive)๊ณผ ๊ฐ์ฒด(Object)์˜ ๊ทผ๋ณธ์ ์ธ ์ฐจ์ด์ ์€ ์›์‹œํƒ€์ž…์€ ๊ฐ’ ์ž์ฒด๋ฅผ ์ €์žฅ ๋˜๋Š” ํ• ๋‹นํ•˜๊ณ  ๊ฐ์ฒด๋Š” ์ €์žฅ๋˜์–ด์žˆ๋Š” ๋ฉ”๋ชจ๋ฆฌ์˜ ์ฃผ์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค๋Š” ์ ์ด๋‹ค. (์ฐธ๊ณ : ์ฝ”์–ด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ)

 

์ผ๋ฐ˜์ ์œผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ฝ”๋“œ๋Š” ๊ฐ์ฒด์˜ ์ฃผ์†Œ๊ฐ’์ด ๋ณต์‚ฌ๋œ๋‹ค. ๊ฐ™์€ ์ฃผ์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— user์˜ ํ”„๋กœํผํ‹ฐ ๊ฐ’์„ ๋ฐ”๋€Œ๋ฉด ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ copyUser์˜ ๊ฐ’๋„ ๋ฐ”๋€Œ๊ฒŒ ๋œ๋‹ค.(๋งจ ์ฒ˜์Œ์— ์–ธ๊ธ‰ํ–ˆ๋˜ ๊ฐ์ฒด๋Š” ๊ฐ™์€ ์ฃผ์†Œ ๊ฐ’์„ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๋Š” ๊ฒƒ์„ ์ƒ๊ฐํ•˜์ž.)

const user = {
    name: "์•ˆ์˜์šฐ",
    age: 27,
} 

const copyUser = user;
user["name"] = "์•ˆ์˜์ค€"
user["age"] = 14

console.log(copyUser);  // { name: '์•ˆ์˜์ค€', age: 14 }

 

์ด์ „์— var, let, const ๋ณ€์ˆ˜์˜ ์ฐจ์ด์ ์„ ๊ณต๋ถ€ํ•˜๋ฉด์„œ ๋– ์˜ค๋ฅธ ์˜๋ฌธ์ ์€ const ๋ณ€์ˆ˜๋กœ ์„ ์–ธ๋œ ๊ฐ์ฒด๋Š” ์„ ์–ธ + ์ดˆ๊ธฐํ™” + ํ• ๋‹น ์„ ๋™์‹œ์— ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ณ€์ˆ˜ ์„ ์–ธ ์ดํ›„ ํ”„๋กœํผํ‹ฐ๊ฐ€ ๋ฐ”๋€Œ๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋‚˜์•ผ ํ•˜๋Š” ๊ฒƒ ์•„๋‹Œ๊ฐ€??๋ผ๊ณ  ํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ, const ๋ณ€์ˆ˜๋Š” user์˜ ๊ฐ’์„ ๊ณ ์ •ํ•˜์ง€๋งŒ ๊ทธ ๋‚ด์šฉ์€ ๊ณ ์ •ํ•˜์ง€ ์•Š๋Š”๋‹ค. (์ฐธ๊ณ : ์ฝ”์–ด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ)


1๏ธโƒฃ Object.assign()

๊ฒฐ๋ก ์ ์œผ๋กœ ์ฐธ์กฐ๊ฐ’์„ ๋ณต์‚ฌํ•˜์ง€ ์•Š๊ณ  ๊ฐ์ฒด ์ž์ฒด๋ฅผ ๋ณต์‚ฌํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด Object.assign() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋˜๊ณ  ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.  const copyUser = Object.assign({}, user) ์—ฌ๊ธฐ์„œ ์ฒซ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜์ธ {} ๋Š” ์ดˆ๊ธฐ๊ฐ’์„ ๋œปํ•˜๊ณ  ๋‘ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ์ฒซ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜(์ดˆ๊ธฐ๊ฐ’ {})์™€ ํ•ฉ์นœ๋‹ค๋Š” ๋œป์ด๋‹ค. ๋‹ค์Œ ์ฝ”๋“œ์—์„œ assign์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ์ฒด๋ฅผ ๋ณต์‚ฌํ•œ ๋ณ€์ˆ˜๋Š” ์›๋ž˜์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•ด๋„ ํ”„๋กœํผํ‹ฐ์˜ ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜์ง€ ์•Š๋Š” ๋ชจ์Šต์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

const user = {
    name: "์•ˆ์˜์šฐ",
    age: 27,
} 

const copyUser = Object.assign({}, user);
user["name"] = "์•ˆ์˜์ค€"
user["age"] = 14

console.log(copyUser);  // { name: '์•ˆ์˜์šฐ', age: 27 }

 

์œ„์—์„œ ์–ธ๊ธ‰ํ•œ const copyUser = Object.assign({}, user)์ฝ”๋“œ์—์„œ ์ฒซ ๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ์ธ {}๋Š” ์ดˆ๊ธฐ๊ฐ’์„ ๋œปํ•œ๋‹ค๊ณ  ํ–ˆ๋Š”๋ฐ, ์—ฌ๊ธฐ์— ์ดˆ๊ธฐ๊ฐ’์„ {} ๋Œ€์‹  ๊ฐ’์ด ๋“ค์–ด์žˆ๋Š” ๊ฐ์ฒด ํ˜•ํƒœ๋กœ ์„ ์–ธํ•ด์„œ ํ•ฉ์น  ์ˆ˜๋„ ์žˆ๋‹ค.

const userInfo = {
    age: 27
}
const addUserInfo = Object.assign({name: "์•ˆ์˜์šฐ"}, userInfo);

console.log(addUserInfo);  // { name: '์•ˆ์˜์šฐ', age: 27 }

 

๋งŒ์•ฝ, ์ดˆ๊ธฐ๊ฐ์ฒด์˜ key์™€ ์ƒˆ๋กœ ๋ฎ์œผ๋ ค๋Š” key ๊ฐ’์ด ์ค‘๋ณต๋˜๋ฉด ์ƒˆ๋กœ ๋„˜์–ด์˜จ ๊ฐ’์„ ๋ฎ์–ด ์”Œ์šด๋‹ค.

const userInfo = {
    name: "์•ˆ์˜์šฐ"
}
const overLapNameInfo = Object.assign(userInfo, {name: "์•ˆ์˜์ค€"});

console.log(overLapNameInfo);  // { name: '์•ˆ์˜์ค€' }

 

๋˜, 2๊ฐœ ์ด์ƒ์˜ ๊ฐ์ฒด๋„ ํ•ฉ์น  ์ˆ˜ ์žˆ๋‹ค. ์ด๋•Œ ๊ฐ€์žฅ ์ขŒ์ธก์— ์žˆ๋Š” ์ดˆ๊ธฐ ๊ฐ์ฒด์— ๋ฎ์–ด ์”Œ์šด๋‹ค.

const mergeMultipleObjs = Object.assign({name: "์•ˆ์˜์šฐ"}, {age: 27}, {gender: "man"});

console.log(mergeMultipleObjs);  // { name: '์•ˆ์˜์šฐ', age: 27, gender: 'man' }

2๏ธโƒฃ Object.keys()

๊ฐ์ฒด์˜ key๊ฐ’์„ ๋ชจ์•„ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜์‹œํ‚ค๋Š” ๋ฉ”์†Œ๋“œ๋‹ค.

const user = {
    name: "์•ˆ์˜์šฐ",
    age: 27,
    gender: "male"
} 
const userKey = Object.keys(user);
console.log(userKey);  // [ 'name', 'age', 'gender' ]

3๏ธโƒฃ Object.values()

๊ฐ์ฒด์˜ value๋ฅผ ๋ชจ์•„ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜์‹œํ‚ค๋Š” ๋ฉ”์†Œ๋“œ๋‹ค.

const user = {
    name: "์•ˆ์˜์šฐ",
    age: 27,
    gender: "male"
} 
const userKey = Object.values(user);
console.log(userKey);  // [ '์•ˆ์˜์šฐ', 27, 'male' ]

4๏ธโƒฃ Object.entries()

key์™€ value๋ฅผ ๋ชจ๋‘ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋‹ค.

const user = {
    name: "์•ˆ์˜์šฐ",
    age: 27,
    gender: "male"
} 
const userKey = Object.values(user);
console.log(userKey);  // [ [ 'name', '์•ˆ์˜์šฐ' ], [ 'age', 27 ], [ 'gender', 'male' ] ]

5๏ธโƒฃ Object.fromEntries()

Object.entries() ์™€ ๋ฐ˜๋Œ€๋กœ key ์™€ value ๊ฐ€ ์Œ์œผ๋กœ ๋ฌถ์—ฌ์žˆ๋Š” ๋ฐฐ์—ด์„ ๊ฐ์ฒด๋กœ ๋ฐ”๊ฟ€ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋‹ค

const changeObjFromArray = Object.fromEntries([ [ 'name', '์•ˆ์˜์šฐ' ], [ 'age', 27 ], [ 'gender', 'male' ] ]);
console.log(changeObjFromArray)   // { name: '์•ˆ์˜์šฐ', age: 27, gender: 'male' }

Reference

  1. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ค‘๊ธ‰ ๊ฐ•์ขŒ #3 - ๊ฐ์ฒด ๋ฉ”์†Œ๋“œ(Object methods), ๊ณ„์‚ฐ๋œ ํ”„๋กœํผํ‹ฐ(Computed property)
  2. ๋ชจ๋˜ JavaScript ํŠœํ† ๋ฆฌ์–ผ
  3. Object.assign() - MDN
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€