๊ด€๋ฆฌ ๋ฉ”๋‰ด

gi_dor

pre-training JavaScript (6) ๋ฐฐ์—ด , ๊ฐ์ฒด (์˜ค๋ธŒ์ ํŠธ) ๋ณธ๋ฌธ

Language/JavaScript

pre-training JavaScript (6) ๋ฐฐ์—ด , ๊ฐ์ฒด (์˜ค๋ธŒ์ ํŠธ)

๊ธฐ๋Œ 2023. 10. 17. 16:12

๐Ÿ’ป ๊ตญ๋น„ 6์ผ์ฐจ


 

๐Ÿ“š ๋ฐฐ์—ด

• ๋ฐฐ์—ด ์ƒ์„ฑํ•˜๊ธฐ 

// ๋ฐฐ์—ด์ƒ์„ฑํ•ด๋ณด์ž

// ๋ฐฐ์—ด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ฐธ์กฐ๋ณ€์ˆ˜์— ๋ฐฐ์—ด๊ฐ์ฒด์˜ ์ฐธ์กฐ๊ฐ’์„ ๋Œ€์ž…
// ์ฐธ์กฐ๋ณ€์ˆ˜๋Š” ๋ฐฐ์—ด๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜๊ณ ์žˆ์œผ๋ฉฐ , ๋ฐฐ์—ด๊ฐ์ฒด์— ์†์„ฑ๊ณผ ๊ธฐ๋Šฅ์„ 
// ์‚ฌ์šฉํ•  ๋•Œ๋Š” ์ฐธ์กฐ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค

let arr1 = [];
let arr2 = ["๊น€์œ ์‹ ","ํ™๊ธธ๋™","์ด์ˆœ์‹ ","๊น€์‹œ๋ฏผ"];
let arr3 = [100 , 100 , 70 ,50 ,70 ,80];

console.log(arr1.length);
console.log(arr2.length);
console.log(arr3.length);


๐Ÿ“Œ ์ฐธ์กฐ๋ณ€์ˆ˜ , ์ฐธ์กฐ๊ฐ’ ?

JavaScript์—์„œ ๋ฐ์ดํ„ฐ ํƒ€์ž… 2๊ฐ€์ง€

  • ์›์‹œ ํƒ€์ž… :  string, number, boolean, null, undefined, symbol
  • ๊ฐ์ฒด ํƒ€์ž… :  Array, Object, Function

 

• ์ฐธ์กฐ๋ณ€์ˆ˜

let arr1 = [];
  • arr1์€ ๋ฐฐ์—ด๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜๋Š” ๋ณ€์ˆ˜ ์ฆ‰  '์ฐธ์กฐ ๋ณ€์ˆ˜' , ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅ๋œ ๋ฐฐ์—ด๊ฐ์ฒด์˜ ์œ„์น˜๋ฅผ ๊ฐ–๊ณ  ์žˆ๋‹ค.
  • ๋ฐฐ์—ด ๊ฐ™์€ ์ฐธ์กฐ ๋ฐ์ดํ„ฐ๋Š” ์‹ค์ œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฉ”๋ชจ๋ฆฌ์˜ ์–ด๋”˜๊ฐ€์— ์ €์žฅํ•˜๊ณ  ๋‚˜์„œ ๊ทธ ์œ„์น˜์ธ '์ฃผ์†Œ' ๋ฅผ ๋ณ€์ˆ˜์— ์ €์žฅํ•œ๋‹ค
let arr = [1, 2, 3];
  • ๋ณ€์ˆ˜ arr์€ ์‹ค์ œ๋กœ ๋ฐฐ์—ด์˜ ๊ฐ’์„ ์ €์žฅํ•˜์ง€ ์•Š๊ณ  ์ €์žฅ๋œ ๋ฉ”๋ชจ๋ฆฌ์˜ ์œ„์น˜๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ์œผ๋ฉฐ
    arr์„ ํ†ตํ•ด ๋ฐฐ์—ด์˜ ์ ‘๊ทผํ•˜๊ฑฐ๋‚˜ ์ถ”๊ฐ€ํ•˜๋Š” ์ž‘์—…์„ ํ• ์ˆ˜ ์žˆ๋‹ค.
  • ๋ฐ˜๋Œ€๋กœ ์›์‹œ๋ฐ์ดํ„ฐ ๋Š” ๋ณ€์ˆ˜์— ์‹ค์ œ ๊ฐ’์ด ์ €์žฅ๋˜์–ด์žˆ๋‹ค  
let number = 1;

let arr =[1,2];

โ—  '๋ณ€์ˆ˜' number๋Š”  1์ด๋ผ๋Š” ๊ฐ’์„ ์ง์ ‘ ์ €์žฅํ•˜๊ณ  ์žˆ๊ณ  arr์€ '์ฐธ์กฐ๋ณ€์ˆ˜'๋กœ ๋ฐฐ์—ด์ด ์ €์žฅ๋œ ๋ฉ”๋ชจ๋ฆฌ์˜ ์ฃผ์†Œ๋ฅผ ์ €์žฅํ•˜๊ณ  ์žˆ๋‹ค

 

•  ์ฐธ์กฐ๊ฐ’

  • ์ฐธ์กฐ ๊ฐ’์€ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅํ•œ ๊ฐ’์ด๋‹ค.
  • ์ฐธ์กฐ ๊ฐ’์„ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜๋ฉด ๋ณ€์ˆ˜์—๋Š” ์ฐธ์กฐ ๊ฐ’์ด ์ €์žฅ๋œ๋‹ค.
let a = { name: "gidor" };
console.log("์ดˆ๊ธฐ a์˜ ๊ฐ’:",a.name);

let b = a;
b.name = "Han";

console.log("์ดํ›„ a์˜๊ฐ’ :",a.name);
console.log("๋ฐ”๋€ b์˜ ๊ฐ’:",b.name);

a์™€ b ๋ณ€์ˆ˜์—๋Š” ๊ฐ์ฒด์˜ ์ฃผ์†Œ๊ฐ€ ์ €์žฅ๋˜์–ด์žˆ๋‹ค
์žฌํ• ๋‹น ์—†์ด ํ•˜๋‚˜์˜ ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•ด ์ง์ ‘์ ์œผ๋กœ ์ˆ˜์ •ํ•˜๋ฉด ๋‹ค๋ฅธ ๋ณ€์ˆ˜์—์„œ๋„ ๊ทธ ๋ณ€๊ฒฝ์‚ฌํ•ญ์ด ๋ฐ˜์˜๋œ๋‹ค.

โ— ์ฐธ์กฐ ํƒ€์ž…์€ ์‹ค์ œ ๊ฐ’์ด ์•„๋‹Œ ์ฐธ์กฐ๊ฐ’์ด ์ „๋‹ฌ๋œ๋‹ค. ์ฆ‰ ์ฐธ์กฐ ๊ฐ’์ด ์ €์žฅ๋œ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๊ฐ€ ์ „๋‹ฌ๋œ๋‹ค.

 

 


• ๋น„์–ด์žˆ๋Š” ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์„œ ๊ฐ’์„ ์ถ”๊ฐ€ํ•˜๊ธฐ.

// ๋ฐฐ์—ด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  , ๋ฐฐ์—ด์— ๊ฐ’์„ ์ €์žฅํ•˜๊ธฐ


// ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•  ๋•Œ , ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๊ฐ’์„ ๋ฏธ๋ฆฌ ์ €์žฅํ•˜๊ธฐ
let arr1 = [100 , 80 ,60 , 70, 100];
let arr2 = ["๊น€์‹œ๋ฏผ","๊น€์œ ์‹ ","๊ณฝ์žฌ์šฐ","์ด์ˆœ์‹ ","๊ถŒ์œจ"];

// ๋น„์–ด์žˆ๋Š” ๋นˆ ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•˜๊ณ  ๊ฐ’์„ ์ถ”๊ฐ€ํ•˜๊ธฐ
let arr3 = []; // ๊ธธ์ด๊ฐ€ 0 ์ธ ๋น„์–ด์žˆ๋Š” ๋ฐฐ์—ด์„ ๋งŒ๋“ค์—ˆ๋‹ค.
arr3[0] = "์„œ์šธ";  // ์ธ๋ฑ์Šค๊ฐ€ 0 ์ธ ๊ณณ์— ๊ฐ’์„ ์ €์žฅํ–ˆ๊ณ 
console.log("arr3์ด ์ฐธ์กฐํ•˜๋Š” ๋ฐฐ์—ด -> "+arr3);
arr3[1] = "์ธ์ฒœ";  // 1๋ฒˆ ๋ฐฉ์— ์ €์žฅ ํ–ˆ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉด ํŽธํ•จ.
console.log("arr3์ด ์ฐธ์กฐํ•˜๋Š” ๋ฐฐ์—ด -> "+arr3);
arr3[2] = "๋Œ€๊ตฌ";  // arr3๊ฐ€ ์ฐธ์กฐํ•˜๋Š” ๋ฐฐ์—ด๊ฐ์ฒด 2๋ฒˆ๋ฐฉ์—  ๊ฐ’์„ ๋Œ€์ž…
console.log("arr3์ด ์ฐธ์กฐํ•˜๋Š” ๋ฐฐ์—ด -> "+arr3);
arr3[3] = "๋ถ€์‚ฐ";
console.log("arr3์ด ์ฐธ์กฐํ•˜๋Š” ๋ฐฐ์—ด -> "+arr3);
arr3[4] = "๊ด‘์ฃผ";
console.log("arr3์ด ์ฐธ์กฐํ•˜๋Š” ๋ฐฐ์—ด -> ",arr3);

 

 

โœ” ์‹ ๊ธฐํ•œ ๋ถ€๋ถ„ 

console.log("arr3์ด ์ฐธ์กฐํ•˜๋Š” ๋ฐฐ์—ด -> "+arr3);
console.log("arr3์ด ์ฐธ์กฐํ•˜๋Š” ๋ฐฐ์—ด -> ",arr3);

// " " ,  ์™€  " " + 
// ๋‘ ๊ฐœ์˜ ๊ฐ’์ด ์„œ๋กœ ๋‹ค๋ฅด๊ฒŒ ๋‚˜์˜ด
// ์ฒซ๋ฒˆ์งธ๋Š” ๋ฌธ์ž์—ด๋กœ ์„œ์šธ , ์ธ์ฒœ , ๋Œ€๊ตฌ , ๋ถ€์‚ฐ
// ๋‘๋ฒˆ์งธ๋Š” ๋ฐฐ์—ด [ '์„œ์šธ', '์ธ์ฒœ', '๋Œ€๊ตฌ', '๋ถ€์‚ฐ', '๊ด‘์ฃผ' ]

 arr3 ๋ฐฐ์—ด์ด ๋ฌธ์ž์—ด ๊ฒฐํ•ฉ์œผ๋กœ ํ˜•๋ณ€ํ™˜์ด ๋˜์–ด์„œ ๋ฌธ์ž์—ด  ์ถœ๋ ฅ์ด ๋˜์—ˆ๋‹ค
๊ฐ„๋‹จํ•œ ๋ฐฐ์—ด์ธ ๊ฒฝ์šฐ ์•„๋ฌด ๋ฌธ์ œ ์—†๋Š”๋ฐ ๋ณต์žกํ•œ ๊ฐ์ฒด๋‚˜ ๋‹ค์ฐจ์› ๋ฐฐ์—ด์˜ ๊ฒฝ์šฐ  ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ์–ป๊ธฐ ํž˜๋“ค๋‹คํ•œ๋‹ค.

๋‘๋ฒˆ์งธ๋Š” arr3 ๋ฐฐ์—ด์ด ๋ฌธ์ž์—ด๋กœ ํ˜•๋ณ€ํ™˜๋˜์ง€ ์•Š๊ณ   ๊ทธ ์ž์ฒด์˜ ํ˜•ํƒœ๋กœ ์ถœ๋ ฅ๋˜์–ด์„œ ์ข€๋” ๋ช…ํ™•ํ•˜๊ฒŒ ์•Œ์ˆ˜ ์žˆ๋‹ค


• ๋ฐฐ์—ด์— ์ €์žฅ๋œ ๊ฐ’์„ ์‚ฌ์šฉํ•˜๊ธฐ

/*
- ๋ฐฐ์—ด๊ฐ์ฒด์— ๊ฐ’์„ ๋Œ€์ž…ํ•˜๊ธฐ
let arr = [];
arr[0] = 100;  ์ฐธ์กฐ๋ณ€์ˆ˜[์ธ๋ฑ์Šค] = ๊ฐ’
arr[1] = 80;
arr[2] = 70;
arr[3] = 40;

- ๋ฐฐ์—ด๊ฐ์ฒด์— ์ €์žฅ๋œ ๊ฐ’์„ ์‚ฌ์šฉํ•˜๊ธฐ
let arr = [100,80,70,40]
console.log(arr[0]); // ์ฐธ์กฐ๋ณ€์ˆ˜[์ธ๋ฑ์Šค]

let total = arr[0] + arr[1] + arr[2] + arr[3];
*/ 


let scores = [100,80,70];
console.log("๊ตญ์–ด์ ์ˆ˜ : "+scores[0]);
console.log("์˜์–ด์ ์ˆ˜ : "+scores[1]);
console.log("์ˆ˜ํ•™์ ์ˆ˜ : "+scores[2]);

let total = scores[0] + scores[1] + scores[2];
let average = total / scores.length;

console.log("์ด์  : " +total);
console.log("ํ‰๊ท  : " + average);

 


• ๋ฐฐ์—ด์— ๋ฐ˜๋ณต๋ฌธ ์‚ฌ์šฉํ•ด๋ณด๊ธฐ

let korScroes = [100 , 80 , 70 ,70 , 90 , 100 , 40 , 50 ,60 , 30];

// ๋ฐฐ์—ด์— ์ €์žฅ๋œ ๋ชจ๋“  ๊ตญ์–ด ์ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๊ธฐ

console.log(korScroes[0]);
console.log(korScroes[1]);
console.log(korScroes[2]);
console.log(korScroes[3]);
console.log(korScroes[4]);
console.log(korScroes[5]);
console.log(korScroes[6]);
console.log(korScroes[7]);
console.log(korScroes[8]);
console.log(korScroes[9]);

( ํšจ์œจ ์™„์ „ ๊ตฌ๋ฆผ~~~~)

 

โœ” ๋งŒ์•ฝ ๋ฐฐ์—ด ๊ธธ์ด๋ฅผ ๋ชจ๋ฅด๊ฑฐ๋‚˜ ๋ฐฐ์—ด์˜ ๊ธธ์ด๊ฐ€ ๋„ˆ~~~๋ฌด ๊ธธ์–ด์„œ ์•Œ์ˆ˜๊ฐ€ ์—†๋‹ค๋ฉด,   length๋ฅผ ์‚ฌ์šฉํ•˜์ž

let korScroes = [100 , 80 , 70 ,70 , 90 , 100 , 40 , 50 ,60 , 30];

// ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ๋ฐฐ์—ด์— ์ €์žฅ๋œ ๋ชจ๋“ ๊ฐ’ ์ถœ๋ ฅํ•˜๊ธฐ

for(let index = 0 ; index < korScroes.length; index++){
    console.log("index : ",index ,"korScores : ", korScroes[index]);
}


// ๊ตญ์–ด ์ ์ˆ˜ ํ•ฉ๊ณ„ ๊ตฌํ•˜๊ธฐ
let total = 0;
for(let index = 0; index < korScroes.length; index++){
    total += korScroes[index];
}
console.log("์ด์  : ",total);

 

 


• ๋ฐฐ์—ด์˜ ์ตœ๋Œ€๊ฐ’ , ์ตœ์†Œ๊ฐ’ ๊ตฌํ•˜๊ธฐ

let arr = [27 , 23, 32, 645, 152 ,65 ,12 , 4];

 

1. ์ตœ๋Œ€๊ฐ’์„ ์ €์žฅํ•˜๋Š” ๋ณ€์ˆ˜ max ์ƒ์„ฑํ•˜๊ณ  ๋ฐฐ์—ด์˜ 0๋ฒˆ์งธ ๊ฐ’์„ ์ €์žฅํ•œ๋‹ค

2. ๋ฐ˜๋ณต๋ฌธ  ๋ฐฐ์—ด์—์„œ index ๋ฒˆ์งธ์˜ ๊ฐ’์„ ์กฐํšŒํ•ด์„œ ๋ณ€์ˆ˜ num์— ๋Œ€์ž…ํ•œ๋‹ค 
๋น„๊ต ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•ด num > max๋ฅผ ์ˆ˜ํ–‰ํ•ด์„œ true ๋กœ ํŒ์ •๋˜๋ฉด  num์˜ ๊ฐ’์„ max์— ๋Œ€์ž…ํ•˜๊ธฐ 

3. ๋ฐ˜๋ณต๋ฌธ ์ข…๋ฃŒ ์ดํ›„ ์ตœ๋Œ€๊ฐ’ ์ถœ๋ ฅํ•˜๊ธฐ
// ๋ฐฐ์—ด์˜ ๊ฐ’ ํƒ์ƒ‰ํ•˜๊ธฐ
// ์ตœ๋Œ€๊ฐ’ , ์ตœ์†Œ๊ฐ’ ๊ตฌํ•˜๊ธฐ

let arr = [27 , 23, 32, 645, 152 ,65 ,12 , 4];

// ๋ฐฐ์—ด์˜ ์ €์žฅ๋œ ๊ฐ’ ์ค‘์— ์ตœ๋Œ€๊ฐ’

/* 
1. ์ตœ๋Œ€๊ฐ’์„ ์ €์žฅํ•˜๋Š” ๋ณ€์ˆ˜ max ์ƒ์„ฑํ•˜๊ณ  ๋ฐฐ์—ด์˜ 0๋ฒˆ์งธ ๊ฐ’์„ ์ €์žฅํ•œ๋‹ค
2. ๋ฐ˜๋ณต๋ฌธ
  ๋ฐฐ์—ด์—์„œ index ๋ฒˆ์งธ์˜ ๊ฐ’์„ ์กฐํšŒํ•ด์„œ ๋ณ€์ˆ˜ num์— ๋Œ€์ž…ํ•œ๋‹ค
  ๋น„๊ต ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•ด num > max๋ฅผ ์ˆ˜ํ–‰ํ•ด์„œ true ๋กœ ํŒ์ •๋˜๋ฉด
  num์˜ ๊ฐ’์„ max์— ๋Œ€์ž…ํ•˜๊ธฐ
  
3. ๋ฐ˜๋ณต๋ฌธ ์ข…๋ฃŒ ์ดํ›„ ์ตœ๋Œ€๊ฐ’ ์ถœ๋ ฅํ•˜๊ธฐ   
 */

let max = arr[0]; // ์ฒซ๋ฒˆ์งธ ๊ฐ’์ด ์ œ์ผ ํฌ๋‹ค๊ฐ€ ์ผ๋‹จ ๊ฐ€์ •ํ•˜์ž
// max์— ๊ฐ’์€ arr[0] ์ธ 27 ์ธ์ƒํƒœ

for(let index = 1; index < arr.length; index++){
    let num = arr[index];   
    if(num > max){
        max = num;
    }
}
console.log("์ตœ๋Œ€๊ฐ’ max :",max);



// ๋ฐฐ์—ด์˜ ์ €์žฅ๋œ ๊ฐ’ ์ค‘์— ์ตœ์†Œ๊ฐ’
/*
1. ์ตœ์†Œ๊ฐ’์„ ์ €์žฅํ•  ๋ณ€์ˆ˜ min ์ƒ์„ฑ ๊ทธ ๊ฐ’์€ ๋ฐฐ์—ด์˜ 0 ๋ฒˆ์งธ ๊ฐ’ arr[0]์œผ๋กœ ์„ค์ •
2. ๋ฐ˜๋ณต๋ฌธ ์‚ฌ์šฉ
    ๋ฐฐ์—ด์—์„œ index ๋ฒˆ์จฐ ๊ฐ’์„ ์กฐํšŒํ•ด์„œ ๋ณ€์ˆ˜ num์— ๋Œ€์ž…ํ•˜๋Š”๋ฐ
    ๋น„๊ต ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•ด num < min ์ด๋ผ๋ฉด
    ๊ทธ ๊ฐ’์„ min์— ๋Œ€์ž…ํ•œ๋‹ค
    (num < min ์ด๋ผ๋ฉด ์ตœ์†Œ๊ฐ’ min ์ €์žฅ๋œ ๊ฐ’๋ณด๋‹ค 
        ์ด๋ฒˆ์— ์ฐพ์€ ๋ฐฐ์—ด์˜ ๊ฐ’์ด ๋” ์ž‘๋‹ค๋Š” 
        ์†Œ๋ฆฌ์ด๋‹ˆ min ์—๋‹ค๊ฐ€  num ๊ฐ’์„ ๋Œ€์ž…)
3. ๋ฐ˜๋ณต๋ฌธ ์ข…๋ฃŒํ›„ ์ตœ์†Œ๊ฐ’ ์ถœ๋ ฅํ•˜๊ธฐ
*/


// 1๋ฒˆ๋ฐฉ๋ฒ•
let min = arr[0];
for(let index = 1; index < arr.length; index++){
    let num = arr[index];
    if(num < min){
        min = num;
    }
}
console.log("์ตœ์†Œ๊ฐ’ min :",min);

// 2๋ฒˆ๋ฐฉ๋ฒ•
let min = arr[0];
for(let index = 1; index < arr.length; index++){
    if(arr[index] < min){
        min = arr[index];
    }
}
console.log("์ตœ์†Œ๊ฐ’ min :",min);

 

 


• ๋ฐฐ์—ด์— ๊ฐ’์ด ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ

1. ์กด์žฌ ์—ฌ๋ถ€๋ฅผ ์ €์žฅํ•˜๋Š” ๋ณ€์ˆ˜ exist๋ฅผ ์ƒ์„ฑ ใ…ก ์ดˆ๊ธฐ๊ฐ’์œผ๋กœ false์ฃผ๊ธฐ 
2. ๊ถ๊ทน์ ์œผ๋กœ true์ธ ๊ฐ’์„ ์ฐพ๊ธฐ ์œ„ํ•ด์„œ false๋ฅผ ์ดˆ๊ธฐ๊ฐ’์œผ๋กœ ์ค€๋‹ค๊ณ ํ•œ๋‹ค.
3. ๋ฐ˜๋ณต์‹œ์ž‘
  - ๋ฐฐ์—ด์˜ 0 ๋ฒˆ์งธ๋ถ€ํ„ฐ ๊ฐ’์„ ์กฐํšŒ 
  - names์˜ ๊ฐ’๊ณผ value ์˜ ๊ฐ’์ด ์ผ์น˜ํ•˜๋ฉด exist์— true๋ฅผ ๋Œ€์ž…ํ•œ๋‹ค
  - ๋ฐ˜๋ณต์ข…๋ฃŒ
4. exist ๊ฐ’ ์ถœ๋ ฅ

 

let names = ["๊น€์œ ์‹ ","๊น€์‹œ๋ฏผ","๊ณฝ์žฌ์šฐ","ํ•œ๋ช…ํšŒ","๊ถŒ์œจ"];


let value = "ํ•œ๋ช…ํšŒ";
let exist = false;

for(let index = 0; index < names.length; index++){
    let name = names[index];
    if(value == name){
        exist = true;
        break;  // ์ผ์น˜ ํ•˜๋Š” ๊ฐ’์„ ๋ฐ˜๊ฒฌํ•œ๋‹ค๋ฉด ๋”์ด์ƒ ๋ฐฐ์—ด์˜ ๊ฐ’ ์กฐ์‚ฌํ•  ํ•„์š” ์—†๋‹ค,
    }
}
console.log("์ฃผ์–ด์ง„ ๊ฐ’์ด ์กด์žฌ ํ•˜๋Š”๊ฐ€ ? : ", exist);

 


์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ๊ฐ’ ์ €์žฅํ•˜๊ธฐ

๋ณ€์ˆ˜ โ—€ ๊ฐ’ ํ•˜๋‚˜ ( ๋ณ€์ˆ˜ ํ•˜๋‚˜๋ฅผ ๋งŒ๋“ค๋ฉด ๊ฐ’์„ ํ•˜๋‚˜ ๋‹ด์„์ˆ˜ ์žˆ๋‹ค)

let a =10;

๋ฐฐ์—ด โ—€ ๊ฐ’ ์—ฌ๋Ÿฌ๊ฐœ ( ๋ฐฐ์—ด ํ•˜๋‚˜๋ฅผ ๋งŒ๋“ค๋ฉด ๊ฐ’์„ ์—ฌ๋Ÿฌ๊ฐœ ๋‹ด์„์ˆ˜ ์žˆ๋‹ค , ์ˆœ์„œ ์žˆ๋Š” ๊ฐ’์˜ ๋ชฉ๋ก + ๊ฐ™์€ ์ข…๋ฅ˜)

let a = [ 10 , 20 , 30]

์˜ค๋ธŒ์ ํŠธ โ—€ ์—ฌ๋Ÿฌ ์ข…๋ฅ˜์— ๊ฐ’ (์ด๋ฆ„ ์žˆ๋Š” ๊ฐ’์˜ ์ง‘ํ•ฉ)


๐Ÿ“š ์˜ค๋ธŒ์ ํŠธ

• ์˜ค๋ธŒ์ ํŠธ ์ƒ์„ฑ

// ์ฑ…์ •๋ณด๋ฅผ ์ €์žฅํ•˜๋Š” ์˜ค๋ธŒ์ ํŠธ ์ƒ์„ฑํ•˜๊ธฐ

let book  = { 
title : '์ž๋ฐ”์˜์ •์„',
author : '๋‚จ๊ถ์„ฑ',
price : 32000,  // ๊ฐ€๊ฒฉ
soldout : false ,  // ํ’ˆ์ ˆ์ƒํƒœ
stock : 5  // ์žฌ๊ณ ๋Ÿ‰
};

//    title  - > key
//    '์ž๋ฐ”์˜์ •์„' -> value

// ์˜ค๋ธŒ์ ํŠธ์— ์ €์žฅ๋œ ๊ฐ’์„ ์ถœ๋ ฅํ•˜๊ธฐ
/* ์˜ค๋ธŒ์ ํŠธ์— ์ €์žฅ๋œ ๊ฐ’ ์กฐํšŒ / ์‚ฌ์šฉํ•˜๊ธฐ
์ฐธ์กฐ๋ณ€์ˆ˜.ํ”„๋กœํผํ‹ฐ
์ฐธ์กฐ๋ณ€์ˆ˜["ํ”„๋กœํผํ‹ฐ๋ช…"]
*/

console.log("์ œ๋ชฉ : "+ book.title);
console.log("์ €์ž : " , book.author);
console.log("๊ฐ€๊ฒฉ : ",book.price);
console.log("ํ’ˆ์ ˆ ์ƒํƒœ : ", book["soldout"]);
console.log("์žฌ๊ณ ๋Ÿ‰ : " , book.stock);

• ๋‹ค์–‘ํ•œ ํƒ€์ž…์˜ ๊ฐ’( ๋ฌธ์ž , ์ˆซ์ž , boolean , ๋ฐฐ์—ด , ๋‹ค๋ฅธ ์˜ค๋ธŒ์ ํŠธ) ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์˜ค๋ธŒ์ ํŠธ ๋งŒ๋“ค๊ธฐ

// ๋‹ค์–‘ํ•œ ํƒ€์ž…์˜ ๊ฐ’( ๋ฌธ์ž , ์ˆซ์ž , boolean , ๋ฐฐ์—ด , ๋‹ค๋ฅธ ์˜ค๋ธŒ์ ํŠธ)๋ฅผ
// ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์˜ค๋ธŒ์ ํŠธ ์ƒ์„ฑํ•˜๊ธฐ


/*
์ž๋™์ฐจ ํŒ๋งค์ ์—์„œ ๊ด€๋ฆฌํ•˜๋Š” ๊ณ ๊ฐ์ •๋ณด
๊ณ ๊ฐ์ด๋ฆ„
๋‚˜์ด 
์ „ํ™”๋ฒˆํ˜ธ 
๋ณด์œ ์ฐจ๋Ÿ‰ - ์ œ์กฐ์‚ฌ
*/
let customer = {
    name : "๊ธฐ์„ ",
    age : 30 ,
    tel : "010 - 0000 - 1234",
    car :{
        company : "ํ˜ผ๋‹ค",
        model : "์Šˆํผ์ปค๋ธŒ",
        year : 2022 ,
        price : 2550000
    } 
}

console.log("๊ณ ๊ฐ๋ช… : ",customer.name);
console.log("๋‚˜์ด :",customer.age);
console.log("์ „ํ™”๋ฒˆํ˜ธ : ",customer.tel);
console.log("๋ณด์œ  ์ฐจ๋Ÿ‰ ์ œ์กฐ์‚ฌ:",customer.car.company)
console.log("๋ณด์œ ์ฐจ๋Ÿ‰ ๋ชจ๋ธ :",customer.car.model);
console.log("๋ณด์œ ์ฐจ๋Ÿ‰ ์—ฐ์‹ :",customer.car.year);


• ๋นˆ ์˜ค๋ธŒ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•ด ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๊ธฐ

// ๋นˆ ์˜ค๋ธŒ์ ํŠธ๋ฅผ ์ƒ์„ฑ ํ•˜๊ณ  ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๊ธฐ
// ์ •๋ณด๋Š” ์ด๋ฆ„๊ณผ ๊ฐ’์˜ ์กฐํ•ฉ


// ์„ฑ์  ์ •๋ณด๋ฅผ ๋‹ด์„ ๋นˆ์˜ค๋ธŒ ์ ํŠธ ์ƒ์„ฑ
// ํ•™์ƒ์ด๋ฆ„ , ๊ตญ์–ด , ์˜์–ด , ์ˆ˜ํ•™ , ์ ์ˆ˜ ์ €์žฅํ•˜๊ธฐ

let score = {};

score.name = "๊ธฐ์„ ";

score["kor"] = 100;
console.log("์„ฑ์ ๊ฐ์ฒด: ", score);

score.eng = 98;
console.log("์„ฑ์ ๊ฐ์ฒด: ", score);

score["math"] = 32;
console.log("์„ฑ์ ๊ฐ์ฒด: ", score);
console.log("๊ฐ’์ด ๋Š˜์–ด๋‚˜๋Š” ๊ฒƒ์„ ํ™•์ธํ• ์ˆ˜ ์žˆ๋‹ค.");

score.total = score.kor + score.eng + score.math;
score.average = score.total / 3;

console.log("ํ•™์ƒ ์ด๋ฆ„ :",score.name);
console.log("์ด์  : ", score.total);
console.log("ํ‰๊ท  : ", score.average);

 


 

• ๋ฒ ์ŠคํŠธ ์…€๋Ÿฌ ์ •๋ณด๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฐ์—ด ์ƒ์„ฑํ•˜๊ธฐ

// ๋ฒ ์ŠคํŠธ ์…€๋Ÿฌ ์ •๋ณด๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฐ์—ด ์ƒ์„ฑํ•˜๊ธฐ

let books = [
    {
        title : "ํŠธ๋ Œ๋“œ ์ฝ”๋ฆฌ์•„ 2024",
        author : "๊น€๋‚œ๋„",
        price : 18000,
        discountRate : 0.1
    },
    {
        title : "ํ“จ์ฒ˜ ์…€ํ”„",
        author : " ๋ฒค์ž๋ฏผ ํ•˜๋”” ",
        price : 19000,
        discountRate : 0.15
    },
    {
        title : "์„ธ์ด๋…ธ์˜ ๊ฐ€๋ฅด์นจ",
        author : " ์„ธ์ด๋…ธ ",
        price : 7000 ,
        discountRate : 0.1
    }

];

// ๋ฐฐ์—ด์— ์ €์žฅ๋œ ๋ฒ ์ŠคํŠธ ์…€๋Ÿฌ ์ •๋ณด ์ถœ๋ ฅํ•˜๊ธฐ
for(let index = 0; index < books.length; index++){
   let x = books[index];
//    console.log(x);  // ๋ชจ๋“  ์ •๋ณด ์ถœ๋ ฅ
console.log(x.title);
}
 

728x90