๐Ÿ† Javascript(2) Object(๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น, ์ „๊ฐœ ์—ฐ์‚ฐ์ž, ๊นŠ์€ ๋ณต์‚ฌ, ์–•์€ ๋ณต์‚ฌ), JSON

Lee Jooamยท2022๋…„ 4์›” 28์ผ
0
post-custom-banner

Object

const info = {
  firstName: "JooAm",
  lastName: "Lee",
  age: 27,
  career: null,
};

console.log(info.firstName, info["age"]);
JooAm 27

๊ฐ์ฒด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด key์™€ value ํ˜•ํƒœ๋กœ ์ด๋ฃจ์–ด์ง„ ์ฐธ์กฐ ํƒ€์ž…์˜ ๋ฐ์ดํ„ฐ์ด๋‹ค.

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

์ „๊ฐœ ์—ฐ์‚ฐ์ž, ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น

const arr = [1, 2, 3, 4, 5];
const obj = { id: 1234, name: "joo" };
const obj2 = { age: 27, name: "Lee" };

console.log(...arr);
console.log({ ...obj } === obj);
console.log({ ...obj, ...obj2 });
1 2 3 4 5
false
{ id: 1234, name: 'Lee', age: 27 }

์ „๊ฐœ ์—ฐ์‚ฐ์ž(spread operator)๋Š” iterableํ•œ ์š”์†Œ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌธ๋ฒ•์ด๋‹ค.

์œ„ ์ฒ˜๋Ÿผ ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์›์†Œ๋ฅผ ๊ฐ๊ฐ ์ „๊ฐœํ•ด์„œ ๋‚˜์—ดํ•˜๊ฑฐ๋‚˜, ๊ฐ์ฒด์˜ ์†์„ฑ๋“ค์„ ์—ด๊ฑฐํ•˜์—ฌ ์ƒˆ๋กœ์šด ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด์— ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋‹ค.

const obj = { id: 1234, name: "joo", age: 27, carrer: null };
const arr = [1, 2, 3, 4, 5];

const [a, b, ...rest] = arr;
const { name, id, ...objRest } = obj;
const { name: newName, id: newId, hi = "hello" } = obj;

console.log(a, b, rest);
console.log(name, id, objRest);
console.log(newName, newId, hi);
1 2 [ 3, 4, 5 ]
joo 1234 { age: 27, carrer: null }
joo 1234 hello

์ „๊ฐœ ์—ฐ์‚ฐ์ž์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น๋„ ์œ ์šฉํ•œ ๋ฌธ๋ฒ• ์ค‘ ํ•˜๋‚˜๋‹ค.

๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค๋‚˜ ๊ฐ์ฒด์˜ ํ‚ค์— ๊ฐ๊ฐ ์ ‘๊ทผํ•ด์„œ ํ• ๋‹นํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ํ•˜๋‚˜์˜ ๋ฌธ์žฅ์œผ๋กœ ํŽธํ•˜๊ฒŒ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋‹ค.

์ด๋•Œ ์ „๊ฐœ ์—ฐ์‚ฐ์ž์™€ ๋น„์Šทํ•œ ๋ฌธ๋ฒ•์ธ ๋‚˜๋จธ์ง€ ์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ํ™œ์šฉํ•˜๋ฉด ๋”ฐ๋กœ ์ง€์ •ํ•˜์ง€ ์•Š์€ ์š”์†Œ๋“ค ๋˜ํ•œ ๋ฌถ์–ด์„œ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ณ€์ˆ˜์˜ ์ด๋ฆ„์„ ๊ฐ์ฒด์˜ key ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์‚ฌ์šฉ์ž๊ฐ€ ์ž„์˜๋กœ ์ง€์ •ํ•  ์ˆ˜๋„ ์žˆ๊ณ  default ๊ฐ’๋„ ์ •ํ•  ์ˆ˜ ์žˆ์œผ๋‹ˆ ์ ์ ˆํ•˜๊ฒŒ ํ™œ์šฉํ•œ๋‹ค๋ฉด ์ •๋ง ํŽธ๋ฆฌํ•œ ๋ฌธ๋ฒ•์ด๋‹ค.

์–•์€ ๋ณต์‚ฌ, ๊นŠ์€ ๋ณต์‚ฌ

const info = {
  firstName: "JooAm",
  lastName: "Lee",
  age: 27,
  career: null,
};
const newInfo = info;
const realNewInfo = { ...info };

console.log(newInfo === info);
newInfo.lastName = "Kim";
console.log(info);
console.log(realNewInfo === info);
realNewInfo.lastName = "Lee";
console.log(info);
true
{ firstName: 'JooAm', lastName: 'Kim', age: 27, career: null }
false
{ firstName: 'JooAm', lastName: 'Kim', age: 27, career: null }

๊ฐ์ฒด๋ฅผ ๋‹ค๋ฃฐ ๋•Œ์—๋Š” ์•ฝ๊ฐ„์˜ ์ฃผ์˜์‚ฌํ•ญ์ด ํ•„์š”ํ•˜๋‹ค. ๊ฐ์ฒด๋Š” ์ฐธ์กฐ ํƒ€์ž…์˜ ๋ณ€์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๋ฅธ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜๋ฉด ๋˜‘๊ฐ™์€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ฐธ์กฐํ•œ๋‹ค.

๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์œ„์ฒ˜๋Ÿผ ์ „๊ฐœ ์—ฐ์‚ฐ์ž๋‚˜ Object์˜ Assign ๋ฉ”์†Œ๋“œ๋ฅผ ํ™œ์šฉํ•ด ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ๋Š” ์‹์œผ๋กœ ํ• ๋‹นํ•ด์•ผํ•œ๋‹ค.

ํ•˜์ง€๋งŒ ์ด๊ฒƒ๋„ ์™„์ „ํ•œ ํ•ด๊ฒฐ์ฑ…์€ ์•„๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ์–•์€ ๋ณต์‚ฌ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

์–•์€ ๋ณต์‚ฌ๋Š” ๊ฐ์ฒด์˜ ๊ฒ‰๋ถ€๋ถ„๋งŒ ๋ณต์‚ฌํ•œ๋‹ค. ์ด ๋ง์˜ ์˜๋ฏธ๋ฅผ ๋ฐ‘์˜ ์˜ˆ์‹œ์—์„œ ์‚ดํŽด๋ณด์ž.

const info = {
  firstName: "JooAm",
  lastName: "Lee",
  age: 27,
  career: null,
  skills: {
    language: ["javascript, c, c++, python"],
    tools: ["React, Redux, Koa"],
    skillToLearn: ["TypeScript, MSW, Express, MongoDB"],
  },
};
const newInfo = info;
const realNewInfo = { ...info };

realNewInfo.age = 30;
realNewInfo.skills.hi = "Hello";

console.log(newInfo);
{
  firstName: 'JooAm',
  lastName: 'Lee',
  age: 27,
  career: null,
  skills: {
    language: [ 'javascript, c, c++, python' ],
    tools: [ 'React, Redux, Koa' ],
    skillToLearn: [ 'TypeScript, MSW, Express, MongoDB' ],
    hi: 'Hello'
  }
}

์ „๊ฐœ ์—ฐ์‚ฐ์ž์™€ ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด๋กœ ํ• ๋‹นํ•œ realNewInfo๋Š” ๊ธฐ์กด์˜ info์™€ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๊ฐ€ ๋‹ค๋ฅด๋‹ค๋Š” ๊ฑธ ์ด์ „์— ํ™•์ธํ–ˆ๋‹ค.

ํ•˜์ง€๋งŒ realNewInfo ๋‚ด๋ถ€์— ๋˜๋‹ค๋ฅธ ์ฐธ์กฐ ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด ์žˆ์„ ๊ฒฝ์šฐ๊ฐ€ ๋ฌธ์ œ๋‹ค.

์œ„์—์„œ realNewInfo์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ •ํ–ˆ๋Š”๋ฐ newInfo์˜ ๊ฐ’์ด ๋ฐ”๋€Œ์—ˆ๋‹ค. skills์— ๋งคํ•‘ ๋œ ๊ฐ’์ด ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

๊ฒ‰๋ถ€๋ถ„๋งŒ ๋ณต์‚ฌํ–ˆ์„ ๋ฟ ์•ˆ์—์„œ๋Š” ๊ฐ™์€ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๋ฅผ ์ฐธ์กฐํ•˜๊ณ  ์žˆ๋Š” ํ˜•ํƒœ์ด๋‹ค.

์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„  ์žฌ๊ท€์ ์œผ๋กœ ๋ณต์‚ฌํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•˜๋˜๊ฐ€, ์•„๋‹ˆ๋ฉด JSON ๊ฐ์ฒด์˜ ๋ฉ”์†Œ๋“œ๋“ค์„ ํ™œ์šฉํ•ด์•ผํ•œ๋‹ค.

JSON

// mock.json
{
  "name": "lee",
  "age": 27,
  "carrer": null,
  "graduated": true
}

JSON์€ ๋ฐ์ดํ„ฐ ์ „์†ก์„ ์œ„ํ•œ format ์ค‘ ํ•˜๋‚˜๋‹ค. ์œ„์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋“ฏ์ด key-value ํ˜•ํƒœ์ด์ง€๋งŒ ๋ฌธ์ž์—ด ํ˜•ํƒœ๋กœ ์ „์†ก๋œ๋‹ค.

๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ๋„คํŠธ์›Œํฌ ํ™˜๊ฒฝ์—์„œ ์•„์ฃผ ์œ ์šฉํ•˜๋‹ค. JSON์€ ๊ฐ’์œผ๋กœ ๋ฌธ์ž์—ด, ์ˆซ์ž, ๋ถˆ๋ฆฌ์–ธ, null, ๋‹ค๋ฅธ ๊ฐ์ฒด(๋ฐฐ์—ด๋„)๋ฅผ ํฌํ•จํ•  ์ˆ˜ ์žˆ๋‹ค.

์œ„์˜ ์˜ˆ์‹œ์—์„œ๋Š” {}๋กœ ๋ฌถ์–ด ๊ฐ์ฒด์ฒ˜๋Ÿผ ํ‘œํ˜„ํ–ˆ์ง€๋งŒ ๋Œ€๊ด„ํ˜ธ๋ฅผ ์ด์šฉํ•ด ๋ฐฐ์—ด๋กœ๋„ ํ™œ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

const mock = require("./mock.json");
// node js ํ™˜๊ฒฝ์—์„œ ์‹คํ–‰ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— require๋กœ ๋ถˆ๋Ÿฌ์˜ด

console.log(mock);

const str = JSON.stringify(mock);

console.log(str);
console.log(typeof str);
console.log(JSON.parse(str));
console.log(typeof JSON.parse(str));
{ name: 'lee', age: 27, carrer: null, graduated: true }
{"name":"lee","age":27,"carrer":null,"graduated":true}
string
{ name: 'lee', age: 27, carrer: null, graduated: true }
object

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” JSON ๊ฐ์ฒด๋ฅผ ํ†ตํ•œ parse์™€ stringify ๋ฉ”์†Œ๋“œ๋ฅผ ์ง€์›ํ•œ๋‹ค. stringify ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ์œ„์™€ ๊ฐ™์ด JSON ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜๋œ๋‹ค.

parse ๋ฉ”์†Œ๋“œ๋Š” ๋ฐ˜๋Œ€๋กœ JSON ํ˜•ํƒœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ฐ์ฒด์ฒ˜๋Ÿผ ๋ณ€ํ™˜ํ•ด์ค€๋‹ค.

์œ„์˜ ์˜ˆ์‹œ์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋“ฏ์ด ๋‘˜์˜ ํƒ€์ž…์„ ํ™•์ธํ•˜๋ฉด ๊ฐ๊ฐ string, object์ธ๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

JSON ๋ฉ”์†Œ๋“œ๋ฅผ ํ™œ์šฉํ•œ ๊นŠ์€ ๋ณต์‚ฌ

const info = {
  firstName: "JooAm",
  lastName: "Lee",
  age: 27,
  career: null,
  skills: {
    language: ["javascript, c, c++, python"],
    tools: ["React, Redux, Koa"],
    skillToLearn: ["TypeScript, MSW, Express, MongoDB"],
  },
};
const newInfo = info;
const realNewInfo = JSON.parse(JSON.stringify(info));

realNewInfo.age = 30;
realNewInfo.skills.hi = "Hello";

console.log(newInfo);
{
  firstName: 'JooAm',
  lastName: 'Lee',
  age: 27,
  career: null,
  skills: {
    language: [ 'javascript, c, c++, python' ],
    tools: [ 'React, Redux, Koa' ],
    skillToLearn: [ 'TypeScript, MSW, Express, MongoDB' ]
  }
}

์•„๊นŒ์˜ ์–•์€ ๋ณต์‚ฌ ์˜ˆ์‹œ๋ฅผ ๋‹ค์‹œ ๋ณด์ž. ์ง€๊ธˆ์€ ์ „๊ฐœ ์—ฐ์‚ฐ์ž๋กœ ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด๋‚ด์ง€ ์•Š๊ณ , JSON ๊ฐ์ฒด์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ํ™œ์šฉํ•˜๊ณ  ์žˆ๋‹ค.

๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•ด๋ณด๋ฉด ์›๋ณธ์„ ์ฐธ์กฐํ•˜๋Š” newInfo์˜ ๊ฐ’์ด ์ „ํ˜€ ๋ณ€ํ•˜์ง€ ์•Š์•˜๋‹ค.

๋งŒ์•ฝ ์ด๊ฒƒ์œผ๋กœ ํ•ด๊ฒฐ๋œ๋‹ค๋ฉด lodash ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ deep clone์ด๋‚˜ ์žฌ๊ท€์ ์ธ ๋ณต์‚ฌ ํ•จ์ˆ˜๋Š” ํ•„์š”์—†๋‹ค. ํ•ด๊ฒฐ์ด ๋œ๋‹ค๋ฉด ๋ง์ด๋‹ค... ๐Ÿ˜‚

const myObj = {
  name: "Jooam",
  carrer: null,
  age: 27,
  sayHello: function () {
    console.log("Hello I'm", this.name);
  },
};

myObj.sayHello();

const newObj = JSON.parse(JSON.stringify(myObj));

console.log(newObj);
Hello I'm Jooam
{ name: 'Jooam', carrer: null, age: 27 }

์œ„์™€ ๊ฐ™์€ ์ƒํ™ฉ์ด ์˜ˆ์™ธ ์ค‘ ํ•˜๋‚˜๋‹ค. ๊ฐ์ฒด์— ๋ฉ”์†Œ๋“œ๋ฅผ ์ •์˜ํ–ˆ๋Š”๋ฐ, JSON ๋ฉ”์†Œ๋“œ๋ฅผ ๊ฑฐ์น˜๊ณ  ๋‚˜๋‹ˆ ์ •์˜ํ•œ ๋ฉ”์†Œ๋“œ๊ฐ€ ์‚ฌ๋ผ์กŒ๋‹ค!

์˜๋„์—์„œ ๋ฒ—์–ด๋‚˜๋Š” ๊ฒฐ๊ณผ๋ฅผ ๋งŒ๋“ค์–ด๋‚ผ ์ˆ˜๋„ ์žˆ์œผ๋‹ˆ ์ ์ ˆํ•˜๊ฒŒ ํŒ๋‹จํ•˜์—ฌ ์–ด๋–ค ๋ฐฉ์‹์œผ๋กœ copyํ• ์ง€ ํŒ๋‹จํ•ด์•ผํ•œ๋‹ค.

Symbol ํƒ€์ž…๋„ stringify ๋ฉ”์†Œ๋“œ์—์„œ ๋ฌด์‹œ๋œ๋‹ค๊ณ  ํ•˜๋Š”๋ฐ, Symbol ํƒ€์ž…์˜ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ๋ณธ ์ ์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— ์ƒ๋žตํ•˜๊ฒ ๋‹ค.

profile
ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ์ž๋กœ ๊ฑธ์–ด๊ฐ€๋Š” ์ค‘์ž…๋‹ˆ๋‹ค.
post-custom-banner

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