โœ…Object.assign()

  • ๊ฐ์ฒด๋“ค์„ ๋ณ‘ํ•ฉํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์†Œ๋“œ
  • ๋ฐฐ์—ด์—์„œ๋Š” ์‹ค์ œ ๋ฐฐ์—ด์˜ ์ด๋ฆ„์„ ๋ถˆ๋Ÿฌ์™€์„œ ์‚ฌ์šฉํ–ˆ์ง€๋งŒ
    object๋Š” ๊ฐ€์ƒ์˜ object๋ฅผ ๋ถˆ๋Ÿฌ๋‹ค๋†“๊ณ  .assign์„ ์ ์šฉํ•˜์—ฌ ์‚ฌ์šฉํ•œ๋‹ค
    (์ •์  ๋ฉ”์†Œ๋“œ์˜ ํŠน์ง• ์ค‘ ํ•˜๋‚˜!)
  • ๊ฐ์ฒด์˜ ์ด๋ฆ„์ด ๊ฒน์น  ๊ฒฝ์šฐ ํ•ฉ์ณ์ง€๋Š”์•„์ด(obj1)๊ฐ’์œผ๋กœ ํ†ต์ผ๋œ๋‹ค
  • ์›๋ณธ : ํ•ฉ์ณ์ง€๋Š” ์•„์ด์˜ ํ•จ์ˆ˜๋Š” ํ•ฉ์ณ์ง€ ์ƒํƒœ๋กœ ๋ณ€ํ•œ๋‹ค
const obj1 = { a: 1, b: 2 };
const obj2 = { b: 3, c: 4 };

const returnObj = Object.assign(obj1, obj2);
 
console.log(obj1);  //{a:1, b:3, c:4}
console.log(returnObj);  //{a:1, b:3, c:4}

โœ… Object.keys()

  • ๊ฐ์ฒด์˜ ket๊ฐ’์„ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋Š” ๋ฉ”์†Œ๋“œ
const user = {
  id: 1,
  name: "dding",
  email: "dding@gmail.com",
};

const keys = Object.keys(user);
console.log(keys);        
// ["id", "name", "email"]์ˆœ์„œ๋กœ ์ถœ๋ ฅ
//["1", "dding", "dding@gmail.com"]

const values = keys.map((key) => user[key]);
console.log(values);
//values๋ผ๋Š” ์ด๋ฆ„์— (user์˜ ํ‚ค๊ฐ’์„ map์„ ์ด์šฉํ•ด์„œ ๋ฐฐ์—ด๋กœ  ๋ฝ‘์•„๋‚ด๋Š” ์ฝ”๋“œ

โœ… ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น(๋น„๊ตฌ์กฐํ™” ํ• ๋‹น)

  • ๊ฐ์ฒด๋‚˜ ๋ฐฐ์—ด์˜ ๊ฐ๊ฐ์˜ ๊ฐ’์„ ๋ถ„ํ•ดํ•˜์—ฌ ๋ณ€์ˆ˜์— ๋„ฃ์–ด์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•
  • user๊ฐ์ฒด์˜ key๊ฐ’์„ ๋ฐ”๋กœ ํ™œ์šฉํ•˜๊ณ  ์‹ถ์„ ๊ฒฝ์šฐ!
    [const{key~~} = ํ™œ์šฉํ•˜๊ณ  ์‹ถ์€ ๊ฐ์ฒด์ด๋ฆ„]
    ์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•˜๋ฉด ํ™œ์šฉํ•˜๊ณ  ์‹ถ์€ ๊ฐ์ฒด์˜ key๊ฐ’์„ ์†์‰ฝ๊ฒŒ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Œ
const user = {
  id: 1,
  name: "tetz",
  email: "xenosign@naver.com",
};
const { id, name, email, address } = user;
// ๊ธฐ๋ณธ๊ฐ’ ์„ค์ • const { id, name, email, address = "KOREA"} = user; ( key๊ฐ’์ด ์—†์„ ๊ฒฝ์šฐ KOREA์ฒ˜๋Ÿผ ์ž„์˜๋กœ ๋„ฃ์–ด์ค„ ์ˆ˜ ์žˆ์Œ)

// ํŠน์ • ๋ณ€์ˆ˜์— ๋„ฃ๊ธฐ const { id, name: tetz, email, address = "KOREA"} = user;
console.log(id);
console.log(name);
console.log(email);
console.log(address);
const fruits = ["์‚ฌ๊ณผ", "๋”ธ๊ธฐ", "๋ง๊ณ ", "์ˆ˜๋ฐ•"];
const [a, b, c, d] = fruits;
console.log(a, b, c, d);

...

โœ… ์ „๊ฐœ ์—ฐ์‚ฐ์ž๋กœ ์‚ฌ์šฉ๋  ๋•Œ

  • ๋ฐฐ์—ด์˜ ๊ฐ’์„ ,์™€ ๋‹จ์œ„๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์ „๊ฐœ์‹œ์ผœ์ฃผ๋Š” ์—ฐ์‚ฐ์ž
const fruits = ["์‚ฌ๊ณผ", "๋ฐ”๋‚˜๋‚˜", "์ˆ˜๋ฐ•"];
console.log(fruits);
console.log(...fruits);
// console.log("์‚ฌ๊ณผ", "๋ฐ”๋‚˜๋‚˜", "์ˆ˜๋ฐ•");

โœ… ๋‚˜๋จธ์ง€ ์—ฐ์‚ฐ์ž๋กœ ์‚ฌ์šฉ๋  ๋•Œ

a,b๊ฐ€ ํŠน๋ณ„ํžˆ ์ค‘์š”ํ•  ๋•Œ (a,b...c)ํ˜•ํƒœ๋กœ ์‚ฌ์šฉ

const fruits = ["์‚ฌ๊ณผ", "๋ฐ”๋‚˜๋‚˜", "์ˆ˜๋ฐ•", "๋ง๊ณ ", "๋”ธ๊ธฐ"];
function conLog(a, b, ...c)  //์‚ฌ๊ณผ,๋ฐ”๋‚˜๋‚˜ ["์ˆ˜๋ฐ•", "๋ง๊ณ ", "๋”ธ๊ธฐ"]
  console.log(a, b, c);
}
conLog(...fruits);  //("์‚ฌ๊ณผ", "๋ฐ”๋‚˜๋‚˜", "์ˆ˜๋ฐ•", "๋ง๊ณ ", "๋”ธ๊ธฐ")

๋ชจ๋‘ ์ค‘์š”ํ•  ๋•Œ (...์ด๋ฆ„)ํ˜•ํƒœ๋กœ ์‚ฌ์šฉ

const fruits = ["์‚ฌ๊ณผ", "๋ฐ”๋‚˜๋‚˜", "์ˆ˜๋ฐ•", "๋ง๊ณ ", "๋”ธ๊ธฐ"];
function conLog(...rest) {
  rest.forEach((element) => {
    console.log(element);
  });
}
conLog(...fruits);
profile
ษช สœแดแด˜แด‡ แด›แด sแดสŸแด แด‡ ส€แด‡แด€สŸ แด˜ส€แดส™สŸแด‡แดs๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ปโค๏ธ

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