๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • What would life be If we had no courage to attemp anything?
Language/JavaScript

[FE/JavaScript] ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น(๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น)

by DevIseo 2022. 7. 6.

๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น

๐Ÿ’ก ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น์ด ๋ญ”๊ฐ€์š”?

๐Ÿ’ก๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น์€ ํฌ๊ฒŒ ์–ด๋–ค ์ข…๋ฅ˜๊ฐ€ ์žˆ๋‚˜์š”?

 

Q.๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น (๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น)

๋ฐฐ์—ด์ด๋‚˜ ๊ฐ์ฒด์˜ ์†์„ฑ์„ ํ•ด์ฒดํ•˜์—ฌ ๊ทธ ๊ฐ’์„ ๊ฐœ๋ณ„ ๋ณ€์ˆ˜์— ๋‹ด์„ ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๋Š” JavaScript ํ‘œํ˜„์‹ - MDN

  • ๊ตฌ์กฐํ™”๋œ ๋ฐฐ์—ด๊ณผ ๊ฐ™์€ ์ดํ„ฐ๋Ÿฌ๋ธ” ๋˜๋Š” ๊ฐ์ฒด๋ฅผ destructuring(๋น„๊ตฌ์กฐํ™”, ๊ตฌ์กฐ ํŒŒ๊ดด)ํ•˜์—ฌ 1๊ฐœ ์ด์ƒ์˜ ๋ณ€์ˆ˜์— ๊ฐœ๋ณ„์ ์œผ๋กœ ํ• ๋‹นํ•˜๋Š” ๊ฒƒ

๐Ÿง iterable?

๋ฐ˜๋ณต ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด(iterable object) ํ˜น์€ ์ค„์—ฌ์„œ iterable ๋‚ด์žฅ๋œ ์ƒ์„ฑ์ž ์ค‘ iterable ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด๋‚ด๋Š” ์ƒ์„ฑ์ž

  • String
  • Array
  • TypedArray
  • Map
  • Set 

Q.๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น ์™œ ์“ฐ์ด์ฃ ?

  • ๋ฐฐ์—ด๊ณผ ๊ฐ™์€ ์ดํ„ฐ๋Ÿฌ๋ธ” ๋˜๋Š” ๊ฐ์ฒด ์ดํ„ฐ๋Ÿด์—์„œ ํ•„์š”ํ•œ ๊ฐ’๋งŒ ์ถ”์ถœํ•˜์—ฌ ๋ณ€์ˆ˜์— ํ• ๋‹น ์‹œ ์œ ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ

Q.๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น์€ ํฌ๊ฒŒ ์–ด๋–ค ์ข…๋ฅ˜๊ฐ€ ์žˆ๋‚˜์š”?

 ๐Ÿ’ก ๋ฐฐ์—ด ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น

 ๐Ÿ’ก ๊ฐ์ฒด ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น

 

1. ๋ฐฐ์—ด ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น

๋ฐฐ์—ด๊ณผ ๊ฐ™์€ ์ดํ„ฐ๋Ÿฌ๋ธ”์—์„œ ํ•„์š”ํ•œ ์š”์†Œ๋งŒ ์ถ”์ถœํ•ด ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์œ ์šฉ

*๏ธโƒฃ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•

//ES5
var arr = [1,2,3];

var one = arr[0];
var two = arr[1];
var three = arr[2];

console.log(one, two, three); //1 2 3
  • ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ๋ฅผ ๋ฐฐ์—ด๋กœ๋ถ€ํ„ฐ ์ถ”์ถœํ•ด 1๊ฐœ ์ด์ƒ์˜ ๋ณ€์ˆ˜์— ํ• ๋‹น
  • ๋ฐฐ์—ด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น์˜ ๋Œ€์ƒ(ํ• ๋‹น๋ฌธ์˜ ์šฐ๋ณ€)์€ ์ดํ„ฐ๋Ÿฌ๋ธ” ํ•ด์•ผํ•˜๋ฉฐ, ํ• ๋‹น ๊ธฐ์ค€์€ ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค (์ฆ‰, ์ˆœ์„œ๋Œ€๋กœ ํ• ๋‹น)
const arr = [1,2,3];

//ES6 ๋ฐฐ์—ด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น
//๋ณ€์ˆ˜ one, two, three๋ฅผ ์„ ์–ธํ•˜๊ณ  ๋ฐฐ์—ด arr๋ฅผ ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ•˜์—ฌ ํ• ๋‹น
//์ด๋•Œ ํ• ๋‹น ๊ธฐ์ค€์€ ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค
const [one,two,three] = arr;
console.log(one,two,three); //1 2 3
  • ํ• ๋‹น ์—ฐ์‚ฐ์ž ์™ผ์ชฝ์— ๊ฐ’์„ ํ• ๋‹น ๋ฐ›์„ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•ด์•ผ ํ•˜๋ฉฐ, ์ด๋•Œ ๋ณ€์ˆ˜๋ฅผ ๋ฐฐ์—ด ๋ฆฌํ„ฐ๋Ÿด ํ˜•ํƒœ๋กœ ์„ ์–ธ
  • ์ด๋•Œ ์šฐ๋ณ€์— ์ดํ„ฐ๋Ÿฌ๋ธ”์„ ํ• ๋‹นํ•˜์ง€ ์•Š์œผ๋ฉด ์—๋Ÿฌ ๋ฐœ์ƒ!
let x,y;
[x,y] = [1,2];
  • ๋ฐฐ์—ด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น์˜ ๋ณ€์ˆ˜ ์„ ์–ธ๋ฌธ์€ ์„ ์–ธ๊ณผ ํ• ๋‹น์„ ๋ถ„๋ฆฌํ•  ์ˆ˜ ์žˆ์Œ
  • ๋‹จ, ์ด๋•Œ const ํ‚ค์›Œ๋“œ๋Š” ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ๊ถŒ์žฅ x
const [a,b] = [1,2];
console.log(a,b) //1,2

const [c,d] = [1];
console.log(c,d) //1 undefined

const [e,f] = [1,2,3];
console.log(e,f); //1 2

const [g, ,h] = [1,2,3];
console.log(g,h) //1 3
  • ๋ฐฐ์—ด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น์˜ ๊ธฐ์ค€์€ ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค์ด๋ฏ€๋กœ ์ˆœ์„œ๋Œ€๋กœ ํ• ๋‹น
  • ์ด๋•Œ ๋ณ€์ˆ˜์˜ ๊ฐœ์ˆ˜์™€ ์ดํ„ฐ๋Ÿฌ๋ธ”์˜ ์š”์†Œ ๊ฐœ์ˆ˜๊ฐ€ ๋ฐ˜๋“œ์‹œ ์ผ์น˜ํ•  ํ•„์š”๋Š” ์—†์Œ
//๊ธฐ๋ณธ๊ฐ’
const [a,b,c = 3] = [1,2];
console.log(a,b,c); //1 2 3

//๊ธฐ๋ณธ๊ฐ’ ๋ณด๋‹ค ํ• ๋‹น๋œ ๊ฐ’์ด ์šฐ์„ 
const [e,f=10,g=3] = [1,2];
console.log(e,f,g); //1,2,3

var h,i;
[h=4,i=7] = [1];
console.log(h); //1
console.log(i); //7
  • ๋ฐฐ์—ด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น์„ ์œ„ํ•œ ๋ณ€์ˆ˜์— ๊ธฐ๋ณธ๊ฐ’์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Œ

 

 

2. ๊ฐ์ฒด ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น

๊ฐ์ฒด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น์€ ๊ฐ์ฒด์—์„œ ํ”„๋กœํผํ‹ฐ ํ‚ค๋กœ ํ•„์š”ํ•œ ํ”„๋กœํผํ‹ฐ ๊ฐ’๋งŒ ์ถ”์ถœํ•ด ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์œ ์šฉ

*๏ธโƒฃ์‚ฌ์šฉ ๋ฐฉ๋ฒ•

๊ฐ์ฒด์˜ ๊ฐ ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ์ฒด๋กœ๋ถ€ํ„ฐ ์ถ”์ถœํ•˜์—ฌ 1๊ฐœ ์ด์ƒ์˜ ๋ณ€์ˆ˜์— ํ• ๋‹น

์ด๋•Œ ๊ฐ์ฒด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น์˜ ๋Œ€์ƒ(ํ• ๋‹น๋ฌธ ์šฐ๋ณ€!)์€ ๊ฐ์ฒด์—ฌ์•ผ ํ•˜๋ฉฐ, ํ• ๋‹น ๊ธฐ์ค€์€ ํ”„๋กœํผํ‹ฐ ํ‚ค

(์ฆ‰, ์ˆœ์„œ๋Š” ์˜๋ฏธ๊ฐ€ ์—†์œผ๋ฉฐ ์„ ์–ธ๋œ ๋ณ€์ˆ˜์˜ ์ด๋ฆ„๊ณผ ํ”„๋กœํผํ‹ฐ ํ‚ค๊ฐ€ ์ผ์น˜ํ•˜๋ฉด ํ• ๋‹น!)

//ES6 ๊ฐ์ฒด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น
const user = { firstName: 'Iseo', lastName: 'Yu'};
//๋ณ€์ˆ˜ lastName, firstName์„ ์„ ์–ธํ•˜๊ณ  user ๊ฐ์ฒด๋ฅผ ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ•˜์—ฌ ํ• ๋‹น
//์ด๋•Œ **ํ”„๋กœํผํ‹ฐ ํ‚ค๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น**์ด ์ด๋ค„์ง!(์ˆœ์„œ๋Š” ์˜๋ฏธx)
const { lastName,firstName } = user;
console.log(firstName,lastName); //Iseo Yu
const {lastName, firstName} = {firstName:'Iseo',lastName:'Yu'};
  • ๋ฐฐ์—ด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ฐ์ฒด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น์„ ์œ„ํ•ด์„œ๋Š” ํ• ๋‹น ์—ฐ์‚ฐ์ž ์™ผ์ชฝ์˜ ํ”„๋กœํผํ‹ฐ ๊ฐ’์„ ํ• ๋‹น ๋ฐ›์„ ๋ณ€์ˆ˜๋กœ ์„ ์–ธ! (๋ณ€์ˆ˜๋ฅผ ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด ํ˜•ํƒœ๋กœ ์„ ์–ธ)
  • ์šฐ๋ณ€์— ๊ฐ์ฒด ๋˜๋Š” ๊ฐ์ฒด๋กœ ํ‰๊ฐ€๋  ์ˆ˜ ์žˆ๋Š” ํ‘œํ˜„์‹(๋ฌธ์ž์—ด, ์ˆซ์ž, ๋ฐฐ์—ด ๋“ฑ)์„ ํ• ๋‹นํ•˜์ง€ ์•Š์œผ๋ฉด ์—๋Ÿฌ
//๋™์น˜
const {lastName, firstName} = user;
const {lastName:lastName, firstName:firstName} = user;
  • ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด ํ˜•ํƒœ๋กœ ์„ ์–ธํ•œ ๋ณ€์ˆ˜๋Š” lastName,firstNmae
  • ํ”„๋กœํผํ‹ฐ ์ถ•์•ฝ ํ‘œํ˜„์„ ํ†ตํ•ด ์„ ์–ธํ•œ ๊ฒƒ
//ํ”„๋กœํผํ‹ฐ ์ถ•์•ฝ(์†์„ฑ๋ช… ์ถ•์•ฝ)
//๊ฐ์ฒด๋ฅผ ์ •์˜ํ•  ๋•Œ key์™€ ํ• ๋‹นํ•˜๋Š” ๋ณ€์ˆ˜์˜ ์ด๋ฆ„์ด ๊ฐ™์œผ๋ฉด ์˜ˆ์‹œ์™€ ๊ฐ™์ด ์ถ•์•ฝ ๊ฐ€๋Šฅ

var books = ['Learning JS', 'Learning Python']
var magazines = ['Vogue', 'Science']
 // ES5
var bookShop = {
 books: books,
 magazines: magazines,
 }
 console.log(bookShop)
////////////////////////////////////////////////////////
const books = ['Learning JS', 'Learning Python']
const magazines = ['Vogue', 'Science']
 // ES6+
const bookShop = {
 books,
 magazines,
 }
 console.log(bookShop)

/*
 {
 books: ['Learning JS', 'Learning Python'],
 magazines: ['Vogue', 'Science']
 }
 */

const user = { firstName: 'Iseo', lastName:'Yu'};

//ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น์ด ์ด๋ค„์ง
//ํ”„๋กœํผํ‹ฐ ํ‚ค๊ฐ€ lastName์ธ ํ”„๋กœํผํ‹ฐ ๊ฐ’์„ ln์— ํ• ๋‹นํ•˜๊ณ ,
//ํ”„๋กœํผํ‹ฐ ํ‚ค๊ฐ€ fistName์ธ ํ”„๋กœํผํ‹ฐ ๊ฐ’์„ fn์— ํ• ๋‹นํ•œ๋‹ค.
const {lastName: ln, firstName: fn} = user;
console.log(fn,ln); //Iseo Yu
  • ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ ํ‚ค์™€ ๋‹ค๋ฅธ ๋ณ€์ˆ˜ ์ด๋ฆ„์œผ๋กœ ๊ฐ’์„ ํ• ๋‹น๋ฐ›์œผ๋ ค๋ฉด ์œ„์™€ ๊ฐ™์ด ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๋ฉด ๋จ

๋˜ํ•œ, ๋ฐฐ์—ด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง์ฒ˜๋Ÿผ ๊ฐ์ฒด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น์„ ์œ„ํ•œ ๋ณ€์ˆ˜์— ๊ธฐ๋ณธ๊ฐ’์„ ์„ค์ • ๊ฐ€๋Šฅ

const {firstName = 'Iseo', lastName} = {lastName:'Yu'};
console.log(firstName,lastName)//Iseo Yu

const {firstName:fn = 'Iseo',lastName:ln} = {lastName:'Yu'};
console.log(fn,ln); //Iseo Yu

๊ฐ์ฒด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น์€ ๊ฐ์ฒด๋ฅผ ์ธ์ˆ˜๋กœ ์ „๋‹ฌ ๋ฐ›๋Š” ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜์—๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

function getUserInfo(user){
	console.log(`์ด๋ฆ„์€ ${user.name}์ด๊ณ  ๋‚˜์ด๋Š” ${user.age}, ์ง์—…์€ ${user.job}์ž…๋‹ˆ๋‹ค.`);
}

getUserInfo({name:"์ด์„œ", age:28, job:"์—†์Œ"});
//์ด๋ฆ„์€ ์ด์„œ์ด๊ณ  ๋‚˜์ด๋Š” 28, ์ง์—…์€ ์—†์Œ์ž…๋‹ˆ๋‹ค.

but, user๊ฐ€ ๋ฐ˜๋ณต ๋˜๋Š” ๊ฑธ ๋ณผ ์ˆ˜ ์žˆ์Œ

๋งค๊ฐœ๋ณ€์ˆ˜ user์— ๊ฐ์ฒด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น์„ ์‚ฌ์šฉ ์‹œ ๋‹ค์Œ์ฒ˜๋Ÿผ ๊ฐ€๋…์„ฑ ์žˆ๊ณ  ๊ฐ„ํŽธํ•œ ์ฝ”๋“œ๋กœ ํ‘œํ˜„ ๊ฐ€๋Šฅ

function getUserInfo({ name, age, job}){
	console.log(`์ด๋ฆ„์€ ${name}์ด๊ณ  ๋‚˜์ด๋Š” ${age}, ์ง์—…์€ ${job}์ž…๋‹ˆ๋‹ค.`);
}
getUserInfo({name:"์ด์„œ", age:28, job:"์—†์Œ"});
//์ด๋ฆ„์€ ์ด์„œ์ด๊ณ  ๋‚˜์ด๋Š” 28, ์ง์—…์€ ์—†์Œ์ž…๋‹ˆ๋‹ค.

'Language > JavaScript' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[JavaScript] API  (0) 2023.01.16
[JavaScript] AJAX & JSON  (0) 2023.01.15
[FE/JavaScript] strict mode  (0) 2022.06.27
[FE/JavaScript]this  (0) 2022.06.23
[FE/JavaScript] ์ƒ์„ฑ์ž ํ•จ์ˆ˜์— ์˜ํ•œ ๊ฐ์ฒด ์ƒ์„ฑ  (0) 2022.06.13

๋Œ“๊ธ€