๐ What You Will Learn
โ๏ธData Structure์ ๊ฐ๋ ํ์์ฑ, ๊ทธ๋ฆฌ๊ณ ๋ค์ํ ์ข ๋ฅ์ ์๋ฃ๊ตฌ์กฐ์ ๋ํด ์ดํดํ๋ค.
โ๏ธSet์ ๊ฐ๋ ๊ณผ ์ฅ์ , ๋จ์ , ๊ทธ๋ฆฌ๊ณ ์ธ์ ์ฌ์ฉํ๋ฉด ์ข์์ง ์ดํดํ๋ค.
โ๏ธDictionary์ ๊ฐ๋ ๊ณผ ์์ฑ๋ฐฉ๋ฒ ๊ทธ๋ฆฌ๊ณ ์ด๋ป๊ฒ ์ ๊ทผํ๋์ง์ดํดํ๋ค.
Set์ array๋ list์ฒ๋ผ ์์ด ์๋ฃ๊ตฌ์กฐ(collection)์ด๋ค.
ํ์ง๋ง set์ ์์ ๊ฐ๋
์ด ์กด์ฌ ํ์ง ์๋ค.
hash๋ ๋จ๋ฐฉํฅ (one way) ์ํธํ.
๋จ๋ฐฉํฅ์ด๋ ํ๋ฒ ์ํธํ ํ๋ฉด ๋ณตํธํ๊ฐ ์ ๋๋ค๋ ๋ป.
์ค์ ๊ฐ์ ๊ธธ์ด์ ์๊ด์์ด hash ๊ฐ์ ์ผ์ ํ ๊ธธ์ด๋ฅผ ๊ฐ์ง๋ค. Hash๋ ์ฃผ๋ก ์์์ ๊ธธ์ด๋ฅผ ๊ฐ๋ ์์์ด ๋ฐ์ดํฐ์ ๋ํด ๊ณ ์ ๋ ๊ธธ์ด์ ๋ฐ์ดํฐ๋ก ๋งคํํ ๋ ์ฌ์ฉ๋๋ค.
let mySet = new Set(['wecode', 'wework', 'wecode']);
// Set { 'wecode', 'wework' } wecode ์ค๋ณต
mySet.add('wecode'); // Set { 'wecode', 'wework' } wecode ์ค๋ณต
mySet.add('wework'); // Set { 'wecode', 'wework' } wework ์ค๋ณต
mySet.add('weplay'); // Set { 'wecode', 'wework', 'weplay' } weplay ์ถ๊ฐ
mySet.forEach((value) => {
console.log(value);
});
// 'wecode'
// 'wework'
// 'weplay'
function isDuplicated(arr) {
var mySet = new Set(arr);
return mySet.size < arr.length;
}
console.log(isDuplicated([1, 1, 2, 3])); // true
console.log(isDuplicated([1, 2, 3])); // false
function uniqueElements(list1, list2) {
var mySet = new Set(list1.concat(list2));
return Array.from(mySet);
}
console.log(uniqueElements([1, 2, 4, 5], [ 2, 3, 5, 6]));
// [1, 2, 4, 5, 3, 6]
// dictionary create 1
let dictionary1 = {"name":["Ryan", "Lee"],
"job":"sw engineer",
"address": {"city":"seoul", "zip_code":"1234"} }
console.log(dictionary1.name[1]); // Lee
console.log(dictionary1.address.city); // seoul
console.log(dictionary1.address.zip_code); // 1234
// dictionary create 2
let dictionary2 = {}
dictionary2["name"] = ["Ryan", "Lee"]
dictionary2["job"] = "sw engineer"
dictionary2["address"] = {"city":"seoul", "zip_code":"1234"}
console.log(dictionary2.name[1]); // Lee
console.log(dictionary2.address.city); // seoul
console.log(dictionary2.address.zip_code); // 1234
// dictionary create 3
let dictionary3 = Object({ "name":["Ryan","Lee"], "job":"sw engineer",
"address":{"city":"seoul","zip_code":"1234"} });
console.log(dictionary3.name[1]); // Lee
console.log(dictionary3.address.city); // seoul
console.log(dictionary3.address.zip_code); // 1234
ํด์๋ ๋จ๋ฐฉํฅ(one way) ์ํธํ. ์ ๋ ฅ ๋ฐ์ดํฐ๋ฅผ ๋ณํํ์ฌ ์๋ณธ ๋ฐ์ดํฐ๋ก ๋ณตํธํํ ์ ์๋๋ก ํ๋ ๊ณผ์ ์ด๋ค. SHA(๋ณด์ ํด์ ์๊ณ ๋ฆฌ์ฆ)์ ์๋ก ๋ค ์ ์๋ค. SHA ํจ์๋ฅผ ์ด์ฉํ์ฌ ๋ฌธ์์ด ๋ฐ์ดํฐ๋ฅผ ์ ๋ ฅ๋ฐ์์ ํด์๊ฐ์ ๊ตฌํ ์ ์๋ค.
var crypto = require('crypto')
var shasum = crypto.createHash('sha1')
shasum.update('wecode')
var hash_value = shasum.digest('hex')
console.log(hash_value);
console.log(hash_value.length);
var shasum2 = crypto.createHash('sha1')
shasum2.update('1234')
var hash_value_1234 = shasum2.digest('hex')
console.log(hash_value_1234);
console.log(hash_value_1234.length);
/* ๊ฒฐ๊ณผ
Hash {
_options: undefined,
writable: true,
readable: true,
[Symbol(kHandle)]: {},
[Symbol(kState)]: { [Symbol(kFinalized)]: false }
}
'283463014a3f8ab829fcf9087ff85d50da1d1bb6'
Hash {
_options: undefined,
writable: true,
readable: true,
[Symbol(kHandle)]: {},
[Symbol(kState)]: { [Symbol(kFinalized)]: false }
}
40
'7110eda4d09e062aa5e4a390b0a572ac0d2c0220'
40
*/
์ด์ฒ๋ผ hash ๊ฐ์ ์ผ์ ํ ๊ธธ์ด๋ฅผ ๊ฐ์ง๊ณ , ์ฃผ๋ก ์์์ ๊ธธ์ด๋ฅผ ๊ฐ๋ ์์์ ๋ฐ์ดํฐ์ ๋ํด ๊ณ ์ ๋ ๊ธธ์ด์ ๋ฐ์ดํฐ๋ก ๋งคํํ ๋ ์ฌ์ฉ๋๋ค.
ํด์ํจ์๋ ๊ฐ์ ๋ฌธ์์ด์ ๋ํด์๋ ๊ฐ์ ์ธ๋ฑ์ค๋ฅผ ๊ตฌํ ์ ์์ด์ผ ํด์ํจ์๋ก ๊ตฌํ ๋ฒํท์ ์ธ๋ฑ์ค๋ฅผ ์ ์ฅ ํ์๋ ๊ตฌํ ์ ์๋ค.