[Snow-ball]프로그래밍(컴퓨터)/자바스크립트(JavaScript)
[JAVASCRIPT] Set Test(셋 테스트)
Snow-ball
2021. 2. 3. 22:17
반응형
1) NewSetTest
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
const NewSetTest = () => {
let setData = new Set()
console.log(setData)
// 출력 : Set(0) { }
// 아무런 데이터 값이 없기 때문에 안뜬다.
let setData2 = new Set()
setData2.add(1)
setData2.add("betazon")
console.log(setData2)
// 출력 : Set(2) {1, "betazon"}
return (
<div className="NewSetTest">
<p>NewSetTest</p>
</div>
)
}
export default NewSetTest
|
cs |
2) NewSetInitTest
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
const NewSetInitTest = () => {
let setData = new Set(["Banana", "Watermelon"])
console.log("NewSetInitTest: " + setData)
// 출력 : [object Set]
console.log("NewSetInitTest: " + setData.size)
// 출력 : 2
return (
<div className="NewSetInitTest">
<p>NewSetInitTest</p>
</div>
)
}
export default NewSetInitTest
|
cs |
3) AddSetTest
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
const AddSetTest = () => {
let setData = new Set()
setData.add("Cherry")
setData.add("strawberry")
setData.add("apple")
// 징 : addSet은 해당배열에 지정한 값이 존재하냐?
// ture false로 반환한다.
console.log(setData)
// 출력 : {"Cherry", "strawberry", "apple"}
console.log("addSetTest: " + setData.has("apple"))
// 출력 : true
console.log("addSetTest: " + setData.has("grape"))
// 출력 : false
return (
<div className="AddSetTest">
<p>
AddSetTest
</p>
</div>
)
}
export default AddSetTest
|
cs |
4) ForEachWithSet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
const ForEachWithSet = () => {
let setData = new Set()
setData.add("Cherry")
setData.add("strawberry")
setData.add("apple")
console.log(setData)
// (val1, val2) or (va1, val3) or (val2, val3)
// 동일하게 출력
// 매개변수명은 달라도 영향은 없다.
// 다만, forEach 동작하면 setData에 데이터값들을 2개ᄊᆔᆨ
// 출력해준다.
setData.forEach(function(val1, val2) {
console.log(val1 + " : " + val2)
})
// 출력 : Cherry : Cherry
// strawberry : strawberry
// apple : apple
setData.forEach(function(val1, val3) {
console.log(val1 + " : " + val3)
})
// 동일 출력
setData.forEach(function(val2, val3) {
console.log(val2 + " : " + val3)
})
// 동일 출력
// arrow function은 ES6에 새롭게 추가된 기능으로 문법 코드가 간결하고
// 직관적이어서 이미 많은 개발자들이 이용하고 있으며, 높은 비율로 function을
// 대체하고 있다.
// setData.forEach(function(val2, val3) { 을
// setData.forEach((val1, val2) => 대체하는 것임.
setData.forEach((val1, val2) =>
console.log("arrow - " + val1 + " : " + val2)
)
// 출력은 "arrow - " 제외하고 동일
return (
<div className="ForEachWithSet">
<p>ForEachWithSet</p>
</div>
)
}
export default ForEachWithSet
|
cs |
5) SetEntryIterationTest
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
const SetEntryIterationTest = () => {
console.log("SetEntryIterationTest Start")
let setData = new Set()
setData.add("Cherry")
setData.add("strawberry")
setData.add("apple")
setData.add("바보")
setData.add("천!재!")
setData.add("!@#$%^")
setData.add(1)
setData.add(999)
console.log(setData)
// 출력 : {"Cherry", "strawberry", .....}
// 모든게 출력이 된다.
var setIter = setData.entries()
for (var entry of setIter) {
console.log(entry)
// 출력 : {"Cherry", "Cherry"}
// ...
// {999, 999}
}
console.log("SetEntryIterationTest Fin")
return (
<div className="SetEntryIterationTest">
<p>SetEntryIterationTest</p>
</div>
)
}
export default SetEntryIterationTest
|
cs |
6) SetKeyIterationTest
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
const SetKeyIterationTest = () => {
console.log("SetKeyIterationTest Start")
let setData = new Set()
setData.add("Cherry")
setData.add("strawberry")
setData.add("apple")
setData.add("바보")
setData.add("천!재!")
setData.add("!@#$%^")
setData.add(1)
setData.add(999)
console.log(setData)
// 출력 : {"Cherry", "strawberry", .....}
// 모든게 출력이 된다.
var setIter = setData.keys()
for (var key of setIter) {
console.log(key)
// 출력 : Cherry
// ...
// 999
}
console.log("SetKeyIterationTest Fin")
return (
<div className="SetIterationTest">
<p>SetKeyIterationTest</p>
</div>
)
}
export default SetKeyIterationTest
|
cs |
베타존 : 네이버쇼핑 스마트스토어
나를 꾸미다 - 인테리어소품 베타존
smartstore.naver.com
반응형