๐ŸŽ› Javascript ๊ฐ์ฒด

Dayยท2022๋…„ 4์›” 4์ผ
0
post-thumbnail

๐Ÿ—„ ๊ฐ์ฒด

๊ฐ์ฒด๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ฐ์ดํ„ฐ ํƒ€์ž… ์ค‘์˜ ํ•˜๋‚˜์ด๊ณ . ์˜์–ด๋กœ๋Š” object

key๊ฐ’๊ณผ value๊ฐ’์„ ์Œ์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๊ณ , ๋ฐฐ์—ด๊ณผ๋Š” ๋‹ค๋ฅด๊ฒŒ ์ˆœ์„œ๊ฐ€ ์ค‘์š”ํ•˜์ง€ ์•Š๋‹ค.

{ key : value }

์˜ˆ๋ฅผ ๋“ค์–ด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฐ์—ด์ด ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•ด๋ดค์„๋•Œ,

let person1 = ['๊ณฝ์ฒ ์šฉ', 'ABํ˜•', 'ENFP', '๋ฐ”๋‹๋ผ๋ผ๋–ผ']

์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ์ •๋ณด๋ฅผ ์–ป๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ•ด๋‹น ๋ฐฐ์—ด์— ์žˆ๋Š” Index number๋ฅผ ๊ธฐ์–ตํ•ด์•ผ ํ•œ๋‹ค.

let person2 = ['์•„์ด์Šค์•„๋ฉ”๋ฆฌ์นด๋…ธ', 'INTP', 'ํ™ํ˜ํŒ”', 'Oํ˜•']

person2๋Š” person1์˜ ์ •๋ณด์™€ ๋‹ค๋ฅธ ์ˆœ์„œ๋กœ ๊ฐ’์ด ๋’ค์„ž์—ฌ ์žˆ์Šต๋‹ˆ๋‹ค.

์‚ฌ๋žŒ์ด ๋‘ ๋ช…์ผ ๋•Œ๋Š” ๊ดœ์ฐฎ์ง€๋งŒ, 100๋ช…์ด๋ผ๋ฉด ์ž๋ฃŒ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ํž˜๋“ค์–ด์ง„๋‹ค.

๊ทธ๋ž˜์„œ ๋‚˜์˜จ ๊ฐœ๋…์ด ๋ฐ”๋กœ ๊ฐ์ฒด์ด๋‹ค.

let person1 = {
  name: '๊ณฝ์ฒ ์šฉ',
  bloodType: 'ABํ˜•',
  mbti: 'ENFP',
  favoriteCoffee: '๋ฐ”๋‹๋ผ๋ผ๋–ผ'
}

key ๊ฐ’์€ ๋”ฐ์˜ดํ‘œ ์•ˆํ•ด๋„ ๋™์ž‘ํ•œ๋‹ค. value๋Š” string ํƒ€์ž… ์ผ๋•Œ๋งŒ ๋”ฐ์˜ดํ‘œ ์“ด๋‹ค.

๊ฐ์ฒด๋กœ ํ‘œํ˜„ํ•˜๋ฉด ์ˆœ์„œ๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— index number๋ฅผ ๊ธฐ์–ตํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.
์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ์ •๋ณด์˜ key๊ฐ’๋งŒ ์•Œ๋ฉด ์ •๋ณด์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ฐ์ฒด ์ ‘๊ทผ

Bracket Notation ๊ณต๋ฐฑ์ด ์žˆ์„ ๋•Œ ์“ด๋‹ค. ๊ฐ์ฒด["key"] = value [ ] ๋”ฐ์˜ดํ‘œ๋ฅผ ์จ์•ผ ํ•จ . ์ˆซ์ž๋กœ ๋œ key ๋„ ์ด๋ ‡๊ฒŒ ์“ด๋‹ค.

๋‹ค ์™ธ์›Œ์•ผ ๋  ํ•„์š” ์—†๊ณ , ์˜ค๋ฅ˜ ๋œจ๋ฉด ํ™•์ธํ•˜๊ณ  ์ˆ˜์ •ํ•˜๋ฉด ๋จ.
๋‹ค์–‘ํ•œ ํ˜•ํƒœ๋กœ key ์ด๋ฆ„ ์ง€์„ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ๋งŒ ์•Œ๋ฉด ๋œ๋‹ค.

Dot Notation ๊ฐ์ฒด.key = value

๊ฐ์ฒด์— ์ถ”๊ฐ€

๊ทธ๋ƒฅ ์ƒˆ๋กœ ์จ์„œ ๋„ฃ์œผ๋ฉด ๋œ๋‹ค

object1.key = value

๊ฐ์ฒด์—์„œ ์ œ๊ฑฐ

delete object1.key

๊ฐ์ฒด์•ˆ์˜ ๊ฐ์ฒด, ๋ฐฐ์—ด ์ ‘๊ทผ

๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ค‘์ฒฉ๋œ ๊ฐ์ฒด์—์„œ 'simple'์„ ์ถœ๋ ฅํ•˜๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ์ ‘๊ทผํ•ด์•ผํ• ๊นŒ?

let nestedObj = {
	type: {
		year: '2019',
		'comment-type': [{
			name: 'simple'
		}]
	}
}
; 

console.log(nestedObj.type['comment-type'][0].name) //์ •๋‹ต

์šฐ์„ , ๊ฐ์ฒด๋ฅผ ํ˜ธ์ถœ ํ•˜๋Š” ๋ฐฉ์‹์„ ์จ์„œ type์— ์ ‘๊ทผํ•œ๋‹ค.
=> nestedObj.type

๊ทธ ๋‹ค์Œ์— 'comment-type'์— ์ ‘๊ทผํ•œ๋‹ค.
=> nestedObj.type['comment-type']

๊ทธ๋ฆฌ๊ณ  ๋‹ค์Œ 'comment-type' property์˜ ๊ฐ’์€ ๋ฐฐ์—ด ๊ตฌ์กฐ(ex> let arr = []; ๊ฐ™์€ ํ˜•์‹)๋กœ ๋˜์–ด์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ธ๋ฑ์Šค๋ฅผ ํ†ตํ•ด์„œ ์ ‘๊ทผํ•œ๋‹ค.
=> nestedObj.type['comment-type'][0]

0๋ฒˆ์งธ ์ธ๋ฑ์Šค์— ์žˆ๋Š” name์ด๋ผ๋Š” ํ”„๋กœํผํ‹ฐ๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” simple์ด๋ผ๋Š” ํ”„๋กœํผํ‹ฐ ๊ฐ’์— ์ ‘๊ทผํ•œ๋‹ค.
=> nestedObj.type['comment-type'][0].name

์ด๊ฒƒ์„ ์ฝ˜์†”๋กœ ํ˜ธ์ถœํ•˜๋ฉด simple์ด ํ™”๋ฉด์— ์ถœ๋ ฅ๋œ๋‹ค.
=> console.log(nestedObj.type['comment-type'][0].name)

๊ฐ์ฒด์—์„œ key ๋กœ value ๊ฐ’์— ์ ‘๊ทผํ•  ๋•Œ, ์ด๋•Œ์˜ key ๋Š” ์‹ค์ œ key ์ด๋ฆ„์„ ์จ์•ผ ํ•œ๋‹ค.
๋‹ค๋ฅธ ๋ณ€์ˆ˜๋กœ ์ง€์ •ํ•œ ๋‹ค์Œ์— ๊ทธ๊ฑธ ์“ฐ๋ฉด ์•ˆ๋จ.

๋˜, const๋กœ ์„ ์–ธํ•œ ๊ฐ์ฒด์˜ ๊ฒฝ์šฐ, ๋‹ค์‹œ ๊ฐ์ฒด ์ž์ฒด๋ฅผ ์„ ์–ธํ•˜๋Š” ๊ฑด ์•ˆ๋˜์ง€๋งŒ, ํ”„๋กœํผํ‹ฐ์˜ ๋‚ด์šฉ์„ ์ˆ˜์ •ํ•˜๊ฑฐ๋‚˜, ์ƒˆ ํ”„๋กœํผํ‹ฐ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์€ ๊ฐ€๋Šฅํ•˜๋‹ค. (์›๋ž˜ const๋Š” ๋‹ค์‹œ ์ˆ˜์ •ํ•˜๋Š” ๊ฑฐ ์ž์ฒด๊ฐ€ ์•ˆ๋๋˜ ๋ณ€์ˆ˜์ž„)


function getData(salesArr,reviewArr,likeArr){

  let sumAmount1 = 0;
  let sumReview1 = 0;
  let sumLike1 = 0;

for(i=0;i<salesArr.length;i++){

   sumAmount1 = sumAmount1 + salesArr[i][1];
   sumReview1 = sumReview1 + reviewArr[i][1];
  sumLike1 = sumLike1 + likeArr[i][1];
  
}

  let a = {sumAmount : sumAmount1 , 
           sumReview : sumReview1 , 
           sumLike : sumLike1
          }
  return  a
      
};
let k = [["20190401", 34], ["20190402", 23], ["20190403", 29]];
let p = [["20190328", 3], ["20190401", 0], ["20190403", 1]];
let f = [["20190328", 98], ["20190401", 102], ["20190403", 125]];

console.log(getData(k,p,f));

์—ฌ๊ธฐ์„œ ํ•ต์‹ฌ์€, ๋ˆ„์  ๊ฐ’์„ ๋‹ค๋ฃฐ ๋•Œ, ์ดˆ๊ธฐ ๋ณ€์ˆ˜ 0์œผ๋กœ ๋งŒ๋“ค์–ด ๋†“๊ณ 
๋ฐ˜๋ณต๋ฌธ ์•ˆ์—์„œ i๋กœ ๋‚˜์˜จ ๊ฐ’๋“ค์„ ๋”ํ•ด์„œ ์žฌํ• ๋‹น ํ•˜๋Š”๊ฑฐ ๊ธฐ์–ตํ•˜์ž

๐Ÿ“ฐ Object.keys()

๊ฐ์ฒด๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ํ‚ค๋“ค์˜ ๋ชฉ๋ก์„ ๋ฐฐ์—ด๋กœ ๋ฆฌํ„ดํ•˜๋Š” ๋ฉ”์†Œ๋“œ

Object.keys(obj) // ['name', 'weight', 'price', 'isFresh']

์ด๋ ‡๊ฒŒ ์‚ฌ์šฉํ•œ๋‹ค.

const keys = Object.keys(obj) // ['name', 'weight', 'price', 'isFresh']

for (let i = 0; i < keys.length; i++) {
const key = keys[i] // ๊ฐ๊ฐ์˜ ํ‚ค
const value = obj[key] // ๊ฐ๊ฐ์˜ ํ‚ค์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ๊ฐ์˜ ๊ฐ’
console.log(value)
}

ES6 ๋ฌธ๋ฒ•์—์„œ๋Š” Object.keys ์ด์™ธ์—๋„ Object ์ƒ์„ฑ์ž์˜ ๋ฉ”์†Œ๋“œ์ธ Object.values , Object.entries ๊ฐ€ ์žˆ๋‹ค.

๐Ÿ“ธ Object.values()

๊ฐ์ฒด์˜ ํ‚ค๊ฐ€ ์•„๋‹Œ ๊ฐ’์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฐฐ์—ด ๋ฆฌํ„ด

const values = Object.values(obj)
// values === ['melon', 4350, 16500, true]

Object.entries()

๊ฐ์ฒด์˜ ํ‚ค์™€ ๊ฐ’์˜ ์Œ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๊ธธ์ด 2์งœ๋ฆฌ ๋ฐฐ์—ด๋กœ ์ด๋ฃจ์–ด์ง„, ๋ฐฐ์—ด์„ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ๋ฐฐ์—ด์—์„œ ์ธ๋ฑ์Šค [0]์˜ ๊ฐ’์€ ๊ฐ๊ฐ์˜ ํ‚ค๋ฅผ,
์ธ๋ฑ์Šค [1]์˜ ๊ฐ’์€ ํ•ด๋‹น ํ‚ค์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์„ ๊ฐ€์ง€๊ฒŒ ๋œ๋‹ค.

  const entries = Object.entries(obj)

/*
entries === [
['name', 'melon'],
['weight', 4350],
['price', 16500],
['isFresh', true]
]
*/


์ œ์ผ ์œ ์šฉํ•˜๊ฒŒ ์“ฐ์ธ๋‹ค. 

for-in

for๋ฌธ ๊ฐ™์€ ๊ฑด๋ฐ, ๊ฐ์ฒด ์ˆœํšŒ ๋ฟ ์•„๋‹ˆ๋ผ, ์ผ๋ฐ˜์  ๋ฐฐ์—ด ์ˆœํšŒํ•  ๋•Œ๋„ ์œ ์šฉ.

์ •๋ฆฌ ๋„ˆ๋ฌด ์ž˜ ๋˜์–ด ์žˆ๋‹ค.
์ผ๋ฐ˜ for ๋ฌธ์„ ์จ๋„ ๋˜์ง€๋งŒ, ๊ฐ์ฒด์—์„œ๋Š” index๊ฐ€ ์—†์–ด์„œ for๋ชป์“ฐ๋‹ˆ๊นŒ ์ด๊ฑธ ์จ์•ผํ•จ

์ด๊ฒƒ์ €๊ฒƒ ๋‹ค ํ•ฉ์ณ์ง„, ์•„์ฃผ ์ค‘์š”ํ•œ ์˜ˆ์ œ!!

const getExamResult = (scores, requiredClasses) => {


let result = { };
let ClassScores = Object.keys(scores);
let ClassNum = Object.values(scores);
 let ff = Object.keys(result);
  
  for(let i in ClassScores){
    
result[ClassScores[i]] = ClassNum[i];
   

  } //result ๋ผ๋Š” ๋นˆ ๊ฐ์ฒด ๋งŒ๋“  ๋‹ค์Œ์—, scores ๊ฐ’๋“ค ์ผ๋‹จ ๋‹ค ๋„ฃ์Œ

  for (let i in requiredClasses){


    if(ff.indexOf(requiredClasses[i]) === -1){

result[requiredClasses[i]] = 0;

    }
  } //  ์—ฌ๊ธฐ๋‹ค๊ฐ€ result์— ์—†๋Š” requiresClasses ๊ฐ’๋“ค ๊นŒ์ง€ ์ถ”๊ฐ€ํ•จ value๋Š” 0์œผ๋กœ. 

  for(let key in result){

let kk = result[key];
    
if( kk === "A+"){
      result[key] = 4.5;
    } else if ( kk === "A"){
      result[key] = 4;
    } else if ( kk === "B+"){
     result[key] = 3.5;
    } else if ( kk === "B"){
      result[key] = 3;
    } else if ( kk === "C+"){
      result[key] = 2.5;
    } else if ( kk === "C"){
     result[key] = 2;
    } else if ( kk === "D+"){
      result[key] = 1.5;
    } else if ( kk === "D"){
      result[key] = 1;
    } else if (kk === "F"){
  result[key] = 0}

}
  
   // ์˜์–ด๋“ค์„ ์ˆซ์ž๋กœ ํ™˜์‚ฐ 

    return result;  // ๊ทธ๋ž˜์„œ ๋งŒ๋“ค์–ด์ง„ result ๋ฆฌํ„ด  
} //์ด๊นŒ์ง€ ํ•จ์ˆ˜  

r = {
  '์ƒํ™œ์†์˜ํšŒ๊ณ„': 'C',
  '๋…ผ๋ฆฌ์ ๊ธ€์“ฐ๊ธฐ': 'B',
  '๋…์ผ๋ฌธํ™”์˜์ดํ•ด': 'B+',
  '๊ธฐ์ดˆ์ˆ˜ํ•™': 'D+',
  '์˜์–ดํšŒํ™”': 'C+',
  '์ธ์ง€๋ฐœ๋‹ฌ์‹ฌ๋ฆฌํ•™': 'A+',
};
y = ['์˜์–ดํšŒํ™”', '๊ธฐ์ดˆ์ˆ˜ํ•™', '๊ณตํ•™์ˆ˜ํ•™', '์ปดํ“จํ„ฐ๊ณผํ•™๊ฐœ๋ก '];

  console.log(getExamResult(r,y));
  

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