๋ฐ˜์‘ํ˜•

JavaScript 36

[JavaScript] array ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ ๊ฐ’ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ•

Intro ์•ˆ๋…•ํ•˜์„ธ์š”. ์ด๋ฒˆ์‹œ๊ฐ„์—๋Š” JavaScript array์—์„œ ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. How to solve the problem ์•„๋ž˜์˜ number ๊ฐ€ ๋“ค์–ด์žˆ๋Š” ๋ฐฐ์—ด์ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ด…์‹œ๋‹ค. const arr = [1, 2, 3, 4, 5]; ๋ฐฐ์—ด์˜ ์ด ๊ธธ์ด์—์„œ -1์„ ๋บ€ ์ธ๋ฑ์Šค๋ฅผ ํ™œ์šฉํ•  ๊ฒฝ์šฐ ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ ๊ฐ’์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. arr[arr.length - 1];

[JavaScript] DOM ์ ‘๊ทผ์„ ํ†ตํ•ด CSS Module์˜ className ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ• (react.js, next.js)

Intro ์•ˆ๋…•ํ•˜์„ธ์š”. ์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” DOM ์ ‘๊ทผ์„ ํ†ตํ•ด CSS Module์˜ classname์„ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค . How to solve the problem ์ €๋Š” next.js ์—์„œ ํŠน์ • className ์„ ๊ฐ€์ ธ์™€์„œ ์ œ๊ฑฐํ•ด์ค˜์•ผ ํ–ˆ์Šต๋‹ˆ๋‹ค. CSS Module์„ ์‚ฌ์šฉํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ํŒŒ์ผ ์ปดํฌ๋„ŒํŠธ๋งˆ๋‹ค ์•„๋ž˜์˜ ํ˜•์‹์œผ๋กœ styles์„ ๊ฐ€์ ธ์™€์„œ ์Šคํƒ€์ผ์„ ์ ์šฉํ•ด์คฌ์Šต๋‹ˆ๋‹ค. import styles from './styles.module.css' ์ฐธ๊ณ ๋กœ css module ๋‚ด๋ถ€ ํ”„๋กœ์„ธ์Šค๋Š” css ํด๋ž˜์Šค๋‚˜ css ํŒŒ์ผ์„ ํ†ตํ•ด ๊ทธ ํด๋ž˜์Šค์˜ ์ด๋ฆ„์„ ๊ณ ์œ ํ•˜๊ฒŒ ๋ฐ”๊ฟ”์ฃผ๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ importํ•˜๋Š” ๋ชจ๋“  css ํŒŒ์ผ์„ ๊ณ ์œ ํ•˜๊ฒŒ ๋งŒ๋“ค์–ด ์ค๋‹ˆ๋‹ค. ๋” ์ž์„ธํ•œ ๋‚ด์šฉ์ด ๊ถ๊ธˆํ•˜์‹  ๋ถ„๋“ค์€ ์ด ๊ธ€ [React ..

[JavaScript] ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ Object array ํŠน์ • ํ•„๋“œ ๊ธฐ์ค€ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•˜๋Š” ๋ฐฉ๋ฒ•

Intro ์•ˆ๋…•ํ•˜์„ธ์š”. ์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” JavaScript ์—์„œ Object array ๋ฅผ ์ •๋ ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. How to do ์•„๋ž˜์˜ ์˜ค๋ธŒ์ ํŠธ ์–ด๋ ˆ์ด๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ด…์‹œ๋‹ค. const objectArray = [ { name: 'aa', year: 2008 }, { name: 'bb', year: 2004 }, { name: 'cc', year: 2003 }, { name: 'dd', year: 1996 }, { name: 'ee', year: 1995 }, { name: 'ff', year: 2015 } ]; javascript์˜ sort() ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด์„œ year ํ•„๋“œ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ์„ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. objectArray.sort((x, y) => x.year - y.yea..

[JavaScript] ํŠน์ • ๋ฌธ์ž์—ด ์ œ๊ฑฐํ•˜๋Š” ๋ฐฉ๋ฒ•

Intro ์•ˆ๋…•ํ•˜์„ธ์š”. ์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ํŠน์ • ๋ฌธ์ž์—ด์„ ์ œ๊ฑฐํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. How to solve the problem ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ํŠน์ • ๋ฌธ์ž์—ด์„ ์ œ๊ฑฐํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์–‘ํ•œ๋ฐ์š”, ๊ทธ ์ค‘์—์„œ ์ €๋Š” replace() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŠน์ • ๋ฌธ์ž์—ด์„ ์ œ๊ฑฐํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. replace()ํ•จ์ˆ˜๋Š” ํŠน์ • ๋ฌธ์ž์—ด์ด ์กด์žฌํ•˜๊ฑฐ๋‚˜ ์ •๊ทœ์‹์— ๋งŒ์กฑํ•  ๊ฒฝ์šฐ, ์›ํ•˜๋Š” ๋ฌธ์ž์—ด๋กœ ๋Œ€์ฒดํ•ด ์ค๋‹ˆ๋‹ค. const str = 'APPLE'; console.log(str.replace('A', '')); // ๋ฌธ์ž์—ด 'A'๋ฅผ ๋นˆ ๊ฐ’์œผ๋กœ ๋Œ€์ฒด, result: PPLE console.log(str.slice('L', '')); // ๋ฌธ์ž์—ด L'๋ฅผ ๋นˆ ๊ฐ’์œผ๋กœ ๋Œ€์ฒด, result: APPE

[JavaScript] moment.js ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ / Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. moment construction falls back to js Date().. ์˜ค๋ฅ˜

Intro moment.js ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‚ ์งœ ๊ฐ์ฒด๋ฅผ ๋‹ค๋ฃจ๋˜ ๋„์ค‘ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค. Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non RFC2822/ISO date formats are discouraged and will be removed in an upcoming major release. Please refer to http://momentjs.com/guides/#/warnings/js-da..

[JavaScript] Math.floor()์ด๋ž€?

Intro JavaScript์˜ Math.floor() ๋ฉ”์†Œ๋“œ์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. Math.floor()? Math.floor() ํ•จ์ˆ˜๋Š” ํ•ญ์ƒ ๋‚ด๋ฆผํ•˜์—ฌ ์ฃผ์–ด์ง„ ์ˆซ์ž๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ๊ฐ€์žฅ ํฐ ์ •์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. Syntax Math.floor(x) Parameters x : A number ์ˆซ์ž Example Math.floor(-Infinity); // -Infinity Math.floor(-45.95); // -46 Math.floor(-45.05); // -46 Math.floor(-0); // -0 Math.floor(0); // 0 Math.floor(4); // 4 Math.floor(45.05); // 45 Math.floor(45.95); // 45 Math.floor(Infini..

[JavaScript] parseInt()์ด๋ž€?

Intro JavaScript์˜ parseInt() ๋ฉ”์†Œ๋“œ์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. parseInt()๋ž€? parseInt() ํ•จ์ˆ˜๋Š” ๋ฌธ์ž์—ด ์ธ์ž๋ฅผ ํŒŒ์‹ฑํ•˜์—ฌ ํŠน์ • ์ง„์ˆ˜(์ˆ˜์˜ ์ง„๋ฒ• ์ฒด๊ณ„์—์„œ ๊ธฐ์ค€์ด ๋˜๋Š” ๊ฐ’)์˜ ์ •์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. Syntax parseInt(string) parseInt(string, radix) Parmaters string : ํŒŒ์‹ฑํ•  ๊ฐ’์ž…๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด์ด ์•„๋‹ ๊ฒฝ์šฐ ToString ์ถ”์ƒ ์—ฐ์‚ฐ์„ ์‚ฌ์šฉํ•ด ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด์˜ ์„ ํ–‰ ๊ณต๋ฐฑ์€ ๋ฌด์‹œํ•ฉ๋‹ˆ๋‹ค. radix (optional) : string์˜ ์ง„์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” 2๋ถ€ํ„ฐ 36๊นŒ์ง€์˜ ์ •์ˆ˜์ž…๋‹ˆ๋‹ค. Example parseInt('0xF', 16) parseInt('F', 16) parseInt('17', 8) parseInt(021..

[JavaScript] ์ต๋ช…ํ•จ์ˆ˜(Anonymous function) ๋ž€ ๋ฌด์—‡์ธ๊ฐ€?

Intro javascript์˜ ์ต๋ช…ํ•จ์ˆ˜์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. Anonymous function ์ต๋ช… ํ•จ์ˆ˜๋Š” ์—ฐ๊ฒฐ๋œ ์ด๋ฆ„์ด ์—†๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ JavaScript์—์„œ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜๊ธฐ ์œ„ํ•ด ํ•จ์ˆ˜ ์ด๋ฆ„ ์•ž์— function ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์ง€๋งŒ JavaScript์˜ ์ต๋ช… ํ•จ์ˆ˜์—์„œ๋Š” ํ•จ์ˆ˜ ์ด๋ฆ„ ์—†์ด function ํ‚ค์›Œ๋“œ๋งŒ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ต๋ช… ํ•จ์ˆ˜๋Š” ์ฒ˜์Œ ์ƒ์„ฑ๋œ ํ›„์—๋Š” ์ ‘๊ทผํ•  ์ˆ˜ ์—†์œผ๋ฉฐ ๊ฐ’์œผ๋กœ ํ•จ์ˆ˜๋กœ ์ €์žฅ๋œ ๋ณ€์ˆ˜๋กœ๋งŒ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ต๋ช… ํ•จ์ˆ˜๋Š” ์—ฌ๋Ÿฌ ์ธ์ˆ˜๋ฅผ ๊ฐ€์งˆ ์ˆ˜๋„ ์žˆ์ง€๋งŒ ํ•˜๋‚˜์˜ ํ‘œํ˜„์‹๋งŒ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Syntax function() { // Function Body } → function ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์„ ์–ธํ•˜์˜€์Šต๋‹ˆ๋‹ค. ( () => { // Function Body... } )..

[JavaScript] Axios ์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ / catch ๋ฌธ์—์„œ Status Code ๋ฐ›์•„์˜ค๋Š” ๋ฐฉ๋ฒ•

Intro Axios์˜ catch() ๋ฌธ์—์„œ Status Code ๋ฅผ ๋ฐ›์•„์˜ค๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋‹ค์Œ์˜ axios ์š”์ฒญ์ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ด…์‹œ๋‹ค. axios .get('foo.example') .then((response) => {}) .catch((error) => { console.log(error); //Logs a string: Error: Request failed with status code 404 }); ๋‹ค์Œ์˜ ๋ฐฉ๋ฒ•์œผ๋กœ console.log ๋กœ ์ฐ์–ด๋ณผ ๊ฒฝ์šฐ ๋‹จ์ˆœ string ๋งŒ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. return ๋œ object๋ฅผ ๋ฐ›๊ณ  ์‹ถ์„ ๊ฒฝ์šฐ ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ๊นŒ์š”? How to solve the problem axios.get('/foo') .catch(functio..

[JavaScript] ํ•œ๋‹ฌ ์ „/ํ›„ ๋‚ ์งœ ๊ตฌํ•˜๊ธฐ

Intro JavaScript ์—์„œ ํ˜„์žฌ ์‹œ๊ฐ„์„ ๊ธฐ์ค€์œผ๋กœ ํ•œ๋‹ฌ ์ „/ํ›„ ๋‚ ์งœ๋ฅผ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. How to solve the problem let now = new Date();// ํ˜„์žฌ ๋‚ ์งœ ๋ฐ ์‹œ๊ฐ„ console.log("ํ˜„์žฌ : ", now); // ํ˜„์žฌ : Fri Sep 30 2022 11:05:32 GMT+0900 (ํ•œ๊ตญ ํ‘œ์ค€์‹œ) let oneMonthAgo = new Date(now.setMonth(now.getMonth() - 1));// ํ•œ๋‹ฌ ์ „ console.log("ํ•œ๋‹ฌ ์ „ : ", oneMonthAgo); // ํ•œ๋‹ฌ ์ „ : Tue Aug 30 2022 11:05:32 GMT+0900 (ํ•œ๊ตญ ํ‘œ์ค€์‹œ) let now = new Date();// ํ˜„์žฌ ๋‚ ์งœ ๋ฐ ์‹œ๊ฐ„..

๋ฐ˜์‘ํ˜•