symbol.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
let obj = {
name: 'green',
age: 30,
}
let id = Symbol('id');
obj[id] = '123';
console.log(obj);
// ▼▼ 결과
// age: 30
// name: "green"
// Symbol(id): "123"
// ↓ 심볼로 준 id는 숨겨짐
console.log(Object.keys(obj)); // ['name', 'age'] ⭐
console.log(Object.values(obj)); // ['green', 30] ⭐
console.log(obj['name']); // 객체의 프로퍼티에 접근 // green
console.log(obj[id]); // '' 빼주기 // 123
console.log(Object.getOwnPropertySymbols(obj)); // [Symbol(id)]
console.log(Reflect.ownKeys(obj)); // ['name', 'age', Symbol(id)]
let clone = Object.assign({}, obj); // 객체 복사 ⭐
console.log(clone);
// {name: 'green', age: 30, Symbol(id): '123'}
// ⭐ 객체 메서드 ( 기억하기 )
// Object.keys(객체) : 객체의 키들을 배열로 반환 (Symbol 제외)
// Object.values(객체) : 객체의 값들을 배열로 반환 (Symbol 제외)
// Object.assign({목표객체}, 복사할 객체) : 복사할 객체를 목표객체에 복사하고, 복사된 객체 반환
// Object.getOwnPropertySymbols(객체) : 객체의 key중 Symbol을 배열로 반환
// Reflect.ownKeys(객체) : 객체의 key들을 배열로 반환 (Symbol 타입, 문자열 타입)
</script>
</body>
</html>
set.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
// ⭐ 사용 ) 방문자 수 기록
// 배열은 올 때마다 추가, set은 한 번만
// 용도 ) 중복 제거하고 다시 배열로 바꿔줌
let set = new Set();
let green = {name: 'green'};
let blue = {name: 'blue'};
let pink = {name: 'pink'};
// 유일무이한 값만 존재, 계속 추가 X
set.add(green);
set.add(blue);
set.add(pink);
set.add(green);
console.log(set);
set.forEach((value, index) => {
console.log(`value는 ${value}이다.`); // value는 [object Object]이다.
console.log(`index는 ${index}이다.`);
})
let numSet = new Set(["a","a",1,1,2,3,4,5]); // 중복제거 ★
let newArr = [...numSet]; // 다시 배열로 받기 ★ 스프레드 구문 ★
console.log(numSet); // 객체이지만 key없고 값 밖에 없음, 배열없음
// Set(6) {'a', 1, 2, 3, 4, …}
console.log(newArr); // (6) ['a', 1, 2, 3, 4, 5]
numSet.forEach((value, index) => {
console.log(`value는 ${value}이다.`); // value는 a~5이다.
console.log(`index는 ${index}이다.`); // index는 a~5이다.
})
</script>
</body>
</html>
ex.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
// ⭐ 문제
// alert 창엔 `Hare, Krishna, :-O` 만 출력되어야 함
// (me)
// let value = new Set([
// "Hare", "Krishna", "Hare", "Krishna",
// "Krishna", "Krishna", "Hare", "Hare", ":-O"
// ]);
// let newArr = [...value]; // 배열로 바꾸기
// console.log(value);
// console.log(newArr);
// alert(newArr);
// (teacher)
let values = [
"Hare", "Krishna", "Hare", "Krishna",
"Krishna", "Krishna", "Hare", "Hare", ":-O"
];
function unique(arr) {
let nameSet = new Set(arr);
console.log(nameSet);
return [...nameSet];
}
alert( unique(values) );
</script>
</body>
</html>
ex_숫자_문자열과영단어.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
// ⭐ 문제) 숫자 문자열과 영단어
// 다음은 숫자의 일부 자릿수를 영단어로 바꾸는 예시입니다.
// 1478 → "one4seveneight"
// 234567 → "23four5six7"
// 10203 → "1zerotwozero3"
// 이렇게 숫자의 일부 자릿수가 영단어로 바뀌어졌거나,
// 혹은 바뀌지 않고 그대로인 문자열 s가 매개변수로 주어집니다.
// s가 의미하는 원래 숫자를 return 하도록 solution 함수를 완성해주세요.
// (예)
// s result
// "one4seveneight" 1478
// "23four5six7" 234567
// "2three45sixseven" 234567
// "123" 123
function solution(s) {
let answer = Number(s.replase(/zero/gm,'0')
.replace(/one/gm,'1')
.replace(/two/gm,'2')
.replace(/three/gm,'3')
.replace(/four/gm,'4')
.replace(/five/gm,'5')
.replace(/six/gm,'6')
.replace(/seven/gm,'7')
.replace(/eight/gm,'8')
.replace(/nine/gm,'9'));
// s = s.replase(/zero/,"0");
return answer;
}
</script>
</body>
</html>
ex_문자열을정수로바꾸기.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
// ⭐ 문자열 s를 숫자로 변환한 결과를 반환하는 함수
function solution(s) {
let answer = 0;
answer = Number(s);
return answer;
}
console.log(solution('-1234'));
</script>
</body>
</html>