๋ณธ๋ฌธ์œผ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ

๐ŸŒˆ Chapter 2 : ๋ฐฐ์—ด๋กœ ๋ฐ์ดํ„ฐ ์ปฌ๋ ‰์…˜์„ ๊ด€๋ฆฌํ•˜๋ผ.

๐ŸŽฏ ๋ฐฐ์—ด๋กœ ์œ ์—ฐํ•œ ์ปฌ๋ ‰์…˜์„ ์ƒ์„ฑํ•˜๋ผ.โ€‹

  • ๋ฐ์ดํ„ฐ ์ปฌ๋ ‰์…˜์„ ๋‹ค๋ฃจ๋Š” ๊ตฌ์กฐ๋กœ Map, Set, WeakMap, ๊ฐ์ฒด, ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๋ฐฐ์—ด์€ ์—ฌ๊ธฐ์ €๊ธฐ ์–ด๋””์—๋‚˜ ๋“ฑ์žฅํ•˜๋Š”๋ฐ, ๋ฐฐ์—ด์— ์ดํ„ฐ๋Ÿฌ๋ธ”(iterable) ์ด ๋‚ด์žฅ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. (Iterator์™€ Generator)
  • iterable์€ ๊ฐ„๋‹จํžˆ ๋งํ•ด ์ปฌ๋ ‰์…˜์˜ ํ˜„์žฌ ์œ„์น˜๋ฅผ ์•Œ๊ณ  ์žˆ๋Š” ์ƒํƒœ์—์„œ ์ปฌ๋ ‰์…˜์˜ ํ•ญ๋ชฉ์„ ํ•œ ๋ฒˆ์— ํ•˜๋‚˜์”ฉ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.
  • String, Array, TypedArray, Map ๋ฐ Set์€ ๋ชจ๋‘ ๋‚ด์žฅ ๋ฐ˜๋ณต ๊ฐ€๋Šฅ ๊ฐ์ฒด์ด๋‹ค. ๊ทธ๋“ค์˜ ํ”„๋กœํ† ํƒ€์ž… ๊ฐ์ฒด๊ฐ€ ๋ชจ๋‘ Symbol.iterator ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
  • ์ปฌ๋ ‰์…˜ ๊ฐœ๋…์˜ ๊ฑฐ์˜ ๋Œ€๋ถ€๋ถ„์„ ๋ฐฐ์—ด ํ˜•ํƒœ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ฆ‰, ๋ฐฐ์—ด์„ ํŠน๋ณ„ํ•œ ์ปฌ๋ ‰์…˜์œผ๋กœ ์‰ฝ๊ฒŒ ๋ณ€ํ™˜ํ•˜๊ฑฐ๋‚˜ ๋‹ค์‹œ ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.
  • ํ‚ค-๊ฐ’ ์Œ์„ ์‚ฌ์šฉํ•ด ๋งต ๊ฐ์ฒด์™€ ๋ฐฐ์—ด ๊ฐ„์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ TC39 ์œ„์›ํšŒ์—์„œ Object.entries()๋ฅผ ์ด์šฉํ•ด์„œ ๊ฐ์ฒด๋ฅผ ํ‚ค-๊ฐ’ ์Œ ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋ช…์„ธ๋ฅผ ํ™•์ •ํ–ˆ๋‹ค.(ES2017)

๐ŸŽฏ Includes()๋กœ ์กด์žฌ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜๋ผ.โ€‹

  • includes๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฐฐ์—ด์— ์žˆ๋Š” ๊ฐ’์˜ ์œ„์น˜๋ฅผ ํ™•์ธํ•˜์ง€ ์•Š๊ณ ๋„ ์กด์žฌ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์•„๋ž˜ ์˜ˆ์ œ์™€ ๊ฐ™์ด ํŠน์ • ๋ฌธ์ž์—ด์„ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋ ค๋ฉด ๋ฌธ์ž์—ด์˜ ์œ„์น˜๋ฅผ ์ฐพ์•„์•ผ ํ•œ๋‹ค.
const sections = ['contact','shipping'];

function displayShipping(sections){
return sections.indexOf('shipping') > -1;
}
// true
  • ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ES2016์—์„œ ์ถ”๊ฐ€๋œ includes๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฒˆ๊ฑฐ๋กœ์šด ๋น„๊ต ์ ˆ์ฐจ๋ฅผ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ๋‹ค.
  • includes๋ผ๋Š” ๋ฐฐ์—ด ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•˜๋ฉด ๊ฐ’์ด ๋ฐฐ์—ด์— ์กด์žฌํ•˜๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•ด์„œ boolean๊ฐ’์œผ๋กœ true ๋˜๋Š” false๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
const sections = ['contact','shipping'];

function displayShipping(sections){
return sections.includes('shipping');
}
// true

๐ŸŽฏ ํŽผ์นจ ์—ฐ์‚ฐ์ž๋กœ ๋ฐฐ์—ด์„ ๋ณธ๋– ๋ผ.โ€‹

  • ๋ฐฐ์—ด์€ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃฐ ๋•Œ ๋งŽ์€ ์œ ์—ฐ์„ฑ์„ ์ œ๊ณตํ•˜์ง€๋งŒ ๋ฐฐ์—ด์—๋Š” ์ˆ˜๋งŽ์€ ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ ํ˜ผ๋ž€์Šค๋Ÿฝ๊ฑฐ๋‚˜ ์กฐ์ž‘๊ณผ ๋ถ€์ˆ˜ ํšจ๊ณผ(side effect)๋กœ ์ธํ•œ ๋ฌธ์ œ์— ๋งž๋‹ฅ๋œจ๋ฆด ์ˆ˜ ์žˆ๋‹ค.
  • ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ํŽผ์นจ ์—ฐ์‚ฐ์ž(Spread syntax)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ตœ์†Œํ•œ์˜ ์ฝ”๋“œ๋กœ ๋ฐฐ์—ด์„ ๋น ๋ฅด๊ฒŒ ์ƒ์„ฑํ•˜๊ณ  ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ํŽผ์นจ ์—ฐ์‚ฐ์ž๋Š” ๋ฐฐ์—ด์—๋งŒ ๊ตญํ•œ๋˜์ง€ ์•Š๊ณ  ๋งต ์ปฌ๋ ‰์…˜์—์„œ๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๊ณ  ํ•จ์ˆ˜์—์„œ๋Š” ํŽผ์นจ ์—ฐ์‚ฐ์ž์˜ ๋‹ค๋ฅธ ํ˜•ํƒœ์ธ ๋‚˜๋จธ์ง€ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๋˜ํ•œ, ํŽผ์นจ ์—ฐ์‚ฐ์ž๋Š” ์ œ๋„ˆ๋ ˆ์ดํ„ฐ๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋‚˜ ํด๋ž˜์Šค ์†์„ฑ์—๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ํŽผ์นจ ์—ฐ์‚ฐ์ž๋Š” ๋‹จ๋…์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜๋Š” ์—†๋‹ค. ์ •๋ณด๋ฅผ ์–ด๋””๋“  ํŽผ์ณ ๋„ฃ์–ด์•ผ ํ•œ๋‹ค.
const cart = ['My Name is SeungMin', 'Helloooooo'];
...cart // error
const copyCart = [...cart]; // ['My Name is SeungMin', 'Helloooooo']
  • ๋‹ค์Œ ์˜ˆ์‹œ๋Š” ๋ฐฐ์—ด์—์„œ ํ•ญ๋ชฉ์„ ์ œ๊ฑฐํ•˜๋ ค๊ณ  ํ•  ๋•Œ ๋ฐ˜๋ณต๋ฌธ๋งŒ ์‚ฌ์šฉํ•œ ๊ฒƒ์ด๋‹ค.
function removeItem(items, removable){
const updated = [];
for(let i = 0; i < items.length; i++){
if(items[i] !== removable){
updated.push(items[i]);
}
}
return updated;
}
  • ์œ„ ์ฝ”๋“œ๋Š” ๋ฐ˜๋ณต๋ฌธ์ด ๋Š˜์–ด๋‚˜๊ณ  ์–ด์ˆ˜์„ ํ•ด์งˆ์ˆ˜๋ก ์ฝ”๋“œ๋ฅผ ์ฝ๊ณ  ์ดํ•ดํ•˜๊ธฐ๊ฐ€ ์–ด๋ ค์›Œ์ง„๋‹ค.
  • splice() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ๋‹ค.
function removeItem(items, removable){
const index = items.indexOf(removable);
items.splice(index, 1);
return items;
}
  • ํ•˜์ง€๋งŒ splice() ๋ฉ”์„œ๋“œ๋Š” ์›๋ณธ ๋ฐฐ์—ด์„ ์กฐ์ž‘ํ•œ๋‹ค.
  • ์›๋ณธ ๋ฐฐ์—ด์„ ์กฐ์ž‘ํ•˜๋Š” ์ž‘์—…์€ ์œ„ํ—˜ํ•œ๋ฐ ํŠนํžˆ ํ•จ์ˆ˜์—์„œ ์‚ฌ์šฉํ•  ๋•Œ ์œ„ํ—˜ํ•˜๋‹ค.
  • ํ•จ์ˆ˜์— ์ „๋‹ฌํ•˜๋Š” ์ •๋ณด๊ฐ€ ๊ทผ๋ณธ์ ์œผ๋กœ ๋‹ฌ๋ผ์ง€๋Š” ๊ฒƒ์„ ์˜ˆ์ธกํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
  • ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์›๋ณธ ๋ฐฐ์—ด์„ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š๊ณ  ๋ฐฐ์—ด์˜ ์ผ๋ถ€๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” slice()๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
function removeItem(items, removable){
const index = items.indexOf(removable);
return items.slice(0, index).concat(items.slice(index + 1));
}
  • ์œ„ ๋ฐฉ๋ฒ•๋„ ๋ฌด์—‡์ด ๋ฐ˜ํ™˜๋˜๋Š”์ง€ ์ •ํ™•ํ•˜์ง€ ์•Š๋‹ค.
  • ๋‹ค๋ฅธ ๊ฐœ๋ฐœ์ž๊ฐ€ ๋ณผ ๋•Œ concat()์œผ๋กœ ๋ฐฐ์—ด ๋‘ ๊ฐœ๋ฅผ ๋ณ‘ํ•ฉํ•ด์„œ ๋ฐฐ์—ด ํ•˜๋‚˜๋ฅผ ์ƒ์„ฑํ•œ๋‹ค๋Š” ์‚ฌ์‹ค์„ ์ดํ•ดํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
  • ์ด๋Ÿฐ ๊ณณ์—์„œ ํŽผ์นจ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ slice() ๋ฉ”์„œ๋“œ์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋ฉด ํ•˜์œ„ ๋ฐฐ์—ด์„ ๋ชฉ๋ก์œผ๋กœ ๋ณ€ํ™˜ํ•ด ๋Œ€๊ด„ํ˜ธ ์•ˆ์— ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.
function removeItem(items, removable){
const index = items.indexOf(removable);
return [...items.slice(0, index), ...items.slice(index + 1)];
}
  • ์œ„ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด ์›๋ณธ ๋ฐฐ์—ด์„ ์กฐ์ž‘ํ•˜์ง€๋„ ์•Š๊ณ , ์ฝ๊ธฐ ์‰ฝ๊ณ  ๊ฐ„๊ฒฐํ•  ๋ฟ ์•„๋‹ˆ๋ผ ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์˜ˆ์ธก ๊ฐ€๋Šฅํ•˜๋‹ค.
  • ์˜๋„๋ฅผ ๊ฐ€์žฅ ์ž˜ ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์„ ํƒํ•˜์ž.
  • ํ•จ์ˆ˜์˜ ์ธ์ˆ˜ ๋ชฉ๋ก์„ ์ƒ์„ฑํ•  ๋•Œ ํŽผ์นจ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
const book = ['์–ด๋ฆฐ์™•์ž','์ƒํ…์ฅํŽ˜๋ฆฌ',20000];
function formatBook(title, author, price){
return `${title} by ${author} ${price}์›`;
}
// ๊ธฐ์กด ์‚ฌ์šฉ ๋ฐฉ๋ฒ•
formatBook(book[0], book[1], book[2]);
// ํŽผ์นจ ์—ฐ์‚ฐ์ž ์‚ฌ์šฉ
formatBook(...book);

๐ŸŽฏ push() ๋ฉ”์„œ๋“œ ๋Œ€์‹  ํŽผ์นจ ์—ฐ์‚ฐ์ž๋กœ ์›๋ณธ ๋ณ€๊ฒฝ์„ ํ”ผํ•ด๋ผ.โ€‹

  • ํŽผ์นจ ์—ฐ์‚ฐ์ž๋กœ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•ด ๋ฐฐ์—ด์— ๋Œ€ํ•œ ์กฐ์ž‘์„ ํ”ผํ•˜์ž.
  • push() ๋ฉ”์„œ๋“œ๋Š” ์ƒˆ๋กœ์šด ํ•ญ๋ชฉ์„ ๋ฐฐ์—ด ๋’ค์— ์ถ”๊ฐ€ํ•ด ์›๋ณธ ๋ฐฐ์—ด์„ ๋ณ€๊ฒฝํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ข‹์ง€ ๋ชปํ•˜๋‹ค.
const reward = {
name: 'The Little Prince',
discount: true,
price: 0,
}

function addFreeGift(cart){
if(cart.length > 2){
cart.push(reward);
return cart;
}
return cart;
}

function summarizeCart(cart){
const discountable = cart.filter(item => item.discount);
if(discountable.length > 1){
return {
error: 'ํ• ์ธ ์ƒํ’ˆ์€ ํ•˜๋‚˜๋งŒ ์ฃผ๋ฌธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.',
};
}
const cartWithReward = addFreeGift(cart);
return {
discount: discountable.length,
items: cartWithReward.length,
cart: cartWithReward,
};
}
  • ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ๋Š” ํ•จ์ˆ˜์— ์ „๋‹ฌํ•œ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š์„ ๊ฒƒ์ด๋ผ๋Š” ์‹ ๋ขฐ๊ฐ€ ํ•„์š”ํ•œ๋ฐ ๋ถ€์ˆ˜ ํšจ๊ณผ๊ฐ€ ์—†๋Š” ํ•จ์ˆ˜๋ฅผ ์ˆœ์ˆ˜ ํ•จ์ˆ˜(pure function)๋ผ๊ณ  ํ•œ๋‹ค.
  • ์œ„ ์ฝ”๋“œ์—์„œ ๋‹ค๋ฅธ ๊ฐœ๋ฐœ์ž๊ฐ€ ๋‹ค๋ฃฐ ๋•Œ ์›๋ณธ ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜์—ˆ์„ ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜์ง€ ๋ชปํ•  ๊ฒƒ์ด๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ํŽผ์นจ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
// ...
function addFreeGift(cart){
if(cart.length > 2){
return [...cart, reward];
}
return cart;
}
// ...
  • ์ถ”๊ฐ€์ ์œผ๋กœ ํŽผ์นจ ์—ฐ์‚ฐ์ž๋กœ ๋ฐฐ์—ด์— ํ•ญ๋ชฉ์„ ์ถ”๊ฐ€ํ•˜๊ณ  ์‚ฌ๋ณธ์„ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•
// shift๋ฅผ ์‚ฌ์šฉํ•œ ์›๋ณธ ์กฐ์ž‘
const titles = ['Little Prince', 'Javascript'];
titles.shift('React');
// ํŽผ์นจ ์—ฐ์‚ฐ์ž
const titles = ['Little Prince', 'Javascript'];
const addTitles = ['React', ...titles];

// slice๋ฅผ ์‚ฌ์šฉํ•œ ๋ณต์‚ฌ
const toCopy = ['Little Prince', 'Javascript'];
const copied = toCopy.slice();
// ํŽผ์นจ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•œ ๋ณต์‚ฌ
const toCopy = ['Little Prince', 'Javascript'];
const copied = [...toCopy];

๐ŸŽฏ ํŽผ์นจ ์—ฐ์‚ฐ์ž๋กœ ์ •๋ ฌ์— ์˜ํ•œ ํ˜ผ๋ž€์„ ํ”ผํ•˜๋ผ.โ€‹

  • ํŽผ์นจ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐฐ์—ด์„ ์ •๋ ฌํ•˜๋ฉด ์—ฌ๋Ÿฌ ๋ฒˆ ์ •๋ ฌํ•ด๋„ ํ•ญ์ƒ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์•„๋ž˜์˜ ๊ฐ์ฒด๊ฐ€ ๋‹ด๊ธด ๋ฐฐ์—ด์„ ์ •๋ ฌํ•ด ๋ณด์ž.
const family = [
{ name: 'Joe', years: 10 },
{ name: 'Theo', years: 5 },
{ name: 'Dyan', years: 10 },
];
  • sort() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ์ˆซ์ž ์ •๋ ฌ๊ณผ ๋ฌธ์ž ์ •๋ ฌ์„ ์œ„ํ•ด ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑํ•˜์ž.
function sortByYears(a, b){
if(a.years === b.years){
return 0;
}
return a.years - b.years;
}

const sortByName = (a, b) => {
if(a.name === b.name){
return 0;
}
return a.name > b.name ? 1 : -1;
};
  • ๋‚˜์ด์ˆœ์œผ๋กœ ์ •๋ ฌ์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.
family.sort(sortByYears);
// {name: "Theo", years: 5}
// {name: "Joe", years: 10}
// {name: "Dyan", years: 10}
  • ์ •๋ ฌ๋œ family๋ฅผ ์ด๋ฆ„์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด ๋ณด์ž.
family.sort(sortByName);
// {name: "Dyan", years: 10}
// {name: "Joe", years: 10}
// {name: "Theo", years: 5}
  • ๋‹ค์‹œ ์ •๋ ฌ๋œ family๋ฅผ ๋‚˜์ด์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด ๋ณด๋ฉด ์ฒ˜์Œ ๋‚˜์ด์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ๊ฒฐ๊ณผ์™€ ์ „ํ˜€ ๋‹ค๋ฅธ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๊ฒŒ ๋œ๋‹ค.
family.sort(sortByYears);
// {name: "Theo", years: 5}
// {name: "Dyan", years: 10}
// {name: "Joe", years: 10}
  • ์ด๋ ‡๋“ฏ ๋งค๋ฒˆ ์ˆœ์„œ๊ฐ€ ๋ฐ”๋€๋‹ค๋ฉด ์‚ฌ์šฉ์ž๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‹ ๋ขฐํ•˜์ง€ ๋ชปํ•  ๊ฒƒ์ด๊ณ  ์šฐ๋ฆฌ๊ฐ€ ์›ํ•œ ๊ฒฐ๊ณผ๋„ ์•„๋‹ˆ๋‹ค.
  • ์ด๋Ÿฐ ๊ฒฝ์šฐ ์›๋ณธ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐ์ž‘ํ•˜์ง€ ์•Š๊ณ  ์‚ฌ๋ณธ์„ ์‚ฌ์šฉํ•ด ์กฐ์ž‘ํ•˜๋ฉด ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.
[...family].sort(sortByYears);
// {name: "Theo", years: 5}
// {name: "Joe", years: 10}
// {name: "Dyan", years: 10}