new Date() 객체를 사용

var today = new Date();
var day = ["Sun","Mon","tue","Wed","Thu","Fri","Sat"];

console.log(today);
console.log(today.getFullYear());
console.log(today.getMonth()+1);  // 0~11
console.log(today.getDate());  // 0~31
console.log(day[today.getDay()]);  // 0~6

console.log(today.getHours());  // 0~23
console.log(today.getMinutes());  // 0~59
console.log(today.getSeconds());  // 0~59
console.log(today.getMilliseconds());  // 0~999

console.log(today.toLocaleDateString());  // mm/dd/yyyy
console.log(today.toLocaleDateString('en-CA'));  // yyyy-mm-dd   en-CA 표기법
console.log(today.toLocaleTimeString());  // hh:mm:ss PM
console.log(today.toLocaleString());   // mm/dd/yyyy, hh:mm:ss PM

'JavaScript' 카테고리의 다른 글

Math methods  (0) 2020.12.19
Object/Array copy  (0) 2020.12.18
JS Sorting  (0) 2020.12.18
Spread operator (스프레드 연산자)  (0) 2020.12.17
Array manipulation  (0) 2020.12.17

Math는 Number 자료형만 가능하며, BigInt와는 사용불가

 

Math.abs(x) : 절대값

Math.ceil(x) : 절상값

Math.floor(x) : 절사값

Math.round(x) : 반올림

Math.max(x,y,z) ; 값중에 가장 큰 값

Math.min(x,y,z) : 값중에 가장 작은 값

Math.pow(x,y) : x의 y제곱

Math.random() : 0에서 1사이의 난수

var num1 = -10.44;
var num2 = 10.44;
var num3 = 10.5;
var arr = [11,3,12,25,20];

/* 절대값 Math.abs(x) */
var absValue = Math.abs(num1)   //  10.44

/* 절상값 Math.ceil(x) */
var ceilValue = Math.ceil(num2)   //  11

/* 절사값 Math.floor(x) */
var floorValue = Math.floor(num2)   //  10

/* 반올림 Math.round(x) */
var roundValue1 = Math.round(num1)   // 10
var roundValue2 = Math.round(num2)   // 11

/* 원소중 최대값 Math.max(x,y,z) */
var maxValue = Math.max(...arr)   //  25

/* 원소중 최소값 Math.min(x,y,z) */
var minValue = Math.min(...arr)   //  3

/* 제곱 Math.pow(x,y) */
var powValue = Math.pow(3,2)   // 9

/* 랜덥 Math.random() */
var randomValue1 = Math.random()   //  난수 0..23728728   0.0023232  0.988883
var randomValue2 = Math.round(Math.random()*100)   // 0에서 100 사이의 랜덤 숫자

'JavaScript' 카테고리의 다른 글

Date  (0) 2020.12.19
Object/Array copy  (0) 2020.12.18
JS Sorting  (0) 2020.12.18
Spread operator (스프레드 연산자)  (0) 2020.12.17
Array manipulation  (0) 2020.12.17

- 일반밸류

/* string copy, 원본과 복사된 값의 간섭이 없다 */
var str = "Tomorrow is on Friday";
var str2 = str;
str = "Tomorrow is not on Saturday";

console.log(str);   //  Tomorrow is not on Saturday
console.log(str2);   //  Tomorrow is on Friday

- 객체 복사

var obj = {name: "john", age: 20, school: "seneca"};
var objNested = {
  name: "john",
  age: 20, 
  school: {name: "seneca", course: "computer", class: ["tue", "thu", "fri"]}, 
  family: ["dad","mom","brother"]
};

/* 객체복사 shallow copy 원본이 같이 변화한다  */
var obj2 = obj;
obj2.name = "tom";
console.log(obj);   //  {name: "tom", age: 20, school: "seneca"}
console.log(obj2);   //  {name: "tom", age: 20, school: "seneca"}

obj.age = 30;
console.log(obj);   //  {name: "tom", age: 30, school: "seneca"}
console.log(obj2);   //  {name: "tom", age: 30, school: "seneca"}

/* spread(...)를 통한 복사 - Deep Copy */
var obj2 = {...obj};
obj.name = "tom";
consol.log(obj);   //  {name: "tom", age: 20, school: "seneca"}
console.log(obj2);   //  {name: "john", age: 20, school: "seneca"}

var obj3 = {...obj, name: "park"};   // {name: "park", age: 20, school: "seneca"}

var objNested2 = {...objNested};   //  nested 값이 같이 변경됨, 진정한 Deep Copy 아니다
objNested.school.name = "humber";
objNested.family[0] = "father";

/* Object.assign({}, obj) - Deep Copy */
var obj2 = Object.assign({}, obj);
obj.name = "tom";
consol.log(obj);   //  {name: "tom", age: 20, school: "seneca"}
console.log(obj2);   //  {name: "john", age: 20, school: "seneca"}

var objNested2 = Object.assign({}, objNested);   //  nested 값이 같이변경됨, 진정한 Deep Copy아니다
objNested.school.name = "humber";
objNested.family[0] = "father";

/* JSON parsing - Deep Copy */
var obj2 = JSON.parse(JSON.stringify(obj));
obj.name = "tom";
consol.log(obj);   //  {name: "tom", age: 20, school: "seneca"}
console.log(obj2);   //  {name: "john", age: 20, school: "seneca"}

var objNested2 = JSON.parse(JSON.stringify(objNested));  // objNested2의 nested 값은 변화없음, 진정한 Deep Copy
objNested.school.name = "humber";
objNested.family[0] = "father";

/* jQuery - Deep Copy */
var obj2 = $.extend(true, {}, obj);
obj.name = "tom";
consol.log(obj);   //  {name: "tom", age: 20, school: "seneca"}
console.log(obj2);   //  {name: "john", age: 20, school: "seneca"}

var objNested2 = $.extend(true,{},objNested);   // objNested2의 nested 값은 변화없음, 진정한 Deep Copy
objNested.school.name = "humber";
objNested.family[0] = "father";

- 배열복사

var arr = ["baseball", "soccer", "basketball", "ballyball", "handball"];
var arrNested = ["baseball", "soccer", "basketball", 
		{america: "football", canada: "hockey", 
    	north: ["america", "canada", "mexico"], 
      south: {win:"brazil", lose:"germany"}
    },
  	"handball", [32,54,30,20], ["add", "substract", "multiple", "devide"]
  ];

/* 배열복사, 원본도 같이 변한다 - shallow copy */
var arr2 = arr;
arr[0] = "softball";
console.log(arr);   //  ["softball", "soccer", "basketball", "ballyball", "handball"]
console.log(arr2);   //  ["softball", "soccer", "basketball", "ballyball", "handball"]

/* Array.from() - Deep Copy */
var arr2 = Array.from(arr);
arr[0] = "softball";
console.log(arr);   //  ["softball", "soccer", "basketball", "ballyball", "handball"]
console.log(arr2);   //  ["baseball", "soccer", "basketball", "ballyball", "handball"]

var arrNested2 = Array.from(arrNested);   //  nested2 값이 같이 변경됨, 진정한 Deep Copy 아니다
arrNested[3].america = "superball";
arrNested[3].north[1] = "CANADA";
arrNested[3].south.lose = "spain";
arrNested[5][3] = 300;
arrNested[6][2] = "minus"
console.log(arrNested);
console.log(arrNested2);

/* slice()사용, splice()는 원본이 []가 됨 - Deep Copy */
var arr2 = arr.slice(0);
arr[0] = "softball"
console.log(arr);   //  ["softball", "soccer", "basketball", "ballyball", "handball"]
console.log(arr2);   //  ["baseball", "soccer", "basketball", "ballyball", "handball"]

var arrNested2 = arrNested.slice(0);   //  nested2 값이 같이 변경됨, 진정한 Deep Copy 아니다
arrNested[3].america = "superball";
arrNested[3].north[1] = "CANADA";
arrNested[3].south.lose = "spain";
arrNested[5][3] = 300;
arrNested[6][2] = "minus"
console.log(arrNested);
console.log(arrNested2);

/* spread 사용 - Deep Copy */
var arr2 = [...arr];
arr[0] = "softball"
console.log(arr);   //  ["softball", "soccer", "basketball", "ballyball", "handball"]
console.log(arr2);   //  ["baseball", "soccer", "basketball", "ballyball", "handball"]

var arrNested2 = [...arrNested];   //  nested2 값이 같이 변경됨, 진정한 Deep Copy 아니다
arrNested[3].america = "superball";
arrNested[3].north[1] = "CANADA";
arrNested[3].south.lose = "spain";
arrNested[5][3] = 300;
arrNested[6][2] = "minus"
console.log(arrNested);
console.log(arrNested2);

/* map() 사용 - Deep Copy */
var arr2 = arr.map(x => x);
arr[0] = "softball"
console.log(arr);   //  ["softball", "soccer", "basketball", "ballyball", "handball"]
console.log(arr2);   //  ["baseball", "soccer", "basketball", "ballyball", "handball"]

var arrNested2 = arrNested.map(x => x);   //  nested2 값이 같이 변경됨, 진정한 Deep Copy 아니다
arrNested[3].america = "superball";
arrNested[3].north[1] = "CANADA";
arrNested[3].south.lose = "spain";
arrNested[5][3] = 300;
arrNested[6][2] = "minus"
console.log(arrNested);
console.log(arrNested2);

/* JSON parsing - Deep Copy */
var arr2 = JSON.parse(JSON.stringify(arr));
arr[0] = "softball"
console.log(arr);   //  ["softball", "soccer", "basketball", "ballyball", "handball"]
console.log(arr2);   //  ["baseball", "soccer", "basketball", "ballyball", "handball"]

var arrNested2 = JSON.parse(JSON.stringify(arrNested));   //  nested2 값 변경없음, 진정한 Deep Copy
arrNested[3].america = "superball";
arrNested[3].north[1] = "CANADA";
arrNested[3].south.lose = "spain";
arrNested[5][3] = 300;
arrNested[6][2] = "minus"
console.log(arrNested);
console.log(arrNested2);

/* jQuery - Deep Copy */
var arr2 = $.extend(true,[],arr);
arr[0] = "softball"
console.log(arr);   //  ["softball", "soccer", "basketball", "ballyball", "handball"]
console.log(arr2);   //  ["baseball", "soccer", "basketball", "ballyball", "handball"]

var arrNested2 = $.extend(true,[],arrNested);   //  nested2 값 변경없음, 진정한 Deep Copy
arrNested[3].america = "superball";
arrNested[3].north[1] = "CANADA";
arrNested[3].south.lose = "spain";
arrNested[5][3] = 300;
arrNested[6][2] = "minus"
console.log(arrNested);
console.log(arrNested2);

'JavaScript' 카테고리의 다른 글

Date  (0) 2020.12.19
Math methods  (0) 2020.12.19
JS Sorting  (0) 2020.12.18
Spread operator (스프레드 연산자)  (0) 2020.12.17
Array manipulation  (0) 2020.12.17

- sort() 함수를 이용(원본 데이터의 변화가 생김)

var strArr = ["hotdog", "hitman", "outgoing", "fourball", "strikout", "foultip", "homerun"]; 
var numArr = [32,12,50,11,18,312,14,32];

/* string  res === strArr */
var res = strArr.sort();   //  ["foultip", "fourball", "hitman", "homerun", "hotdog", "outgoing", "strikout"]

/* string 특정위치부터 정렬시  res === strArr */
var res = strArr.sort(function(a,b){
  var aa = a.substr(3);   //  4번째 자리부터 비교
  var bb = b.substr(3);
  return aa == bb?0:aa<bb?-1:1;
});   //  ["hotdog", "homerun", "outgoing", "strikout", "foultip", "hitman", "fourball"]

/* reverse() */
var res = strArr.reverse();  // ["homerun", "foultip", "strikout", "fourball", "outgoing", "hitman", "hotdog"]


/* number  res === numArr */
var res = numArr.sort();   //  [11, 12, 14, 18, 312, 32, 32, 50] 숫자를 앞자리부터 비교하므로 정렬이 안됨
var res = numArr.sort(function(a,b){return a-b;});   // [11, 12, 14, 18, 32, 32, 50, 312] DESC는 b-a

'JavaScript' 카테고리의 다른 글

Math methods  (0) 2020.12.19
Object/Array copy  (0) 2020.12.18
Spread operator (스프레드 연산자)  (0) 2020.12.17
Array manipulation  (0) 2020.12.17
Manipulation(String, Array)  (0) 2020.12.16

+ Recent posts