February 20, 2020
🐥 추가 예정
reduce
🐥 정규표현식 다시보기!
String.prototype.length
const greeting = ' Hello world! '
greeting.trim()
// 'Hello world!';
const str = '01234'
str.substring(1, 3)
// parameter : (처음 index, 마지막 index)
// "12"
str.substr(1, 2)
// parameter : (처음 index, 길이)
// "12"
const str = '아구아구'
str.charAt(2)
// "아"
Array 도 가능
const str = '아구아구'
str.indexOf('아')
// 0
str.indexOf('뀨')
// -1
Array 도 가능
const str = '아구아구'
str.lastIndexOf('아')
// 2
str.lastIndexOf('뀨')
// -1
const str = 'Taeny'
str.toLowerCase()
// taeny
str.toUpperCase()
// TAENY
const str = 'hi hello bye'
const arr = str.split(' ')
// ["hi", "hello", "bye"]
const str = 'cat is barking'
str.replace('cat', 'dog')
// "dog is barking"
let arr = ['b', 'a', 'c']
arr.sort((a, b) => a.localeCompare(b))
// ["a", "b", "c"]
'a'.charCodeAt(0)
// 해당문자에 대한 아스키코드 반환
// 97
String.fromCharCode(97)
// 아스키코드에 대한 문자 반환
// 'a'
const obj = { name: '태은', age: 3 }
const keys = Object.keys(obj)
// ['name', 'age']
const values = Object.values(obj)
// ['태은', 3]
const obj = { a: 1, b: 2 }
const obj2 = { b: 4, c: 5 }
Object.assign(obj, obj2)
// obj { a: 1, b: 4, c: 5 }
const obj = { x: 1 }
obj.y = 2
// obj {x: 1, y: 2}
const obj = { x: 1, y: 2 }
delete obj.y
// obj {x: 1}
const array1 = ['a', 'b', 'c']
const array2 = []
array1.forEach(element => console.log(element))
// a b c
array1.forEach(element => array2.push(element))
// array2 ['a', 'b', 'c']
String도 가능
const arr = [1, 2, 3, 4]
const arr2 = arr.concat([5, 6])
// [1,2,3,4,5,6]
const arr3 = arr.concat(5)
// [1,2,3,4,5]
String도 가능
const arr = [1, 2, 3, 4]
const arr2 = arr.slice(1, 3)
// parameter : (처음 index, 마지막 index)
// arr2 [2,3]
const arr = [1, 2, 3, 4]
arr.splice(1, 1, 'add')
// parameter : (처음 index, 삭제할 요소 개수, 추가할 요소들...)
// arr [1, "add", 3, 4]
const arr = [1, 2, 3, 4]
const arr2 = arr.filter(v => v > 2)
// parameter : (조건 함수)
// arr2 [3,4]
const arr = [1, 2, 3, 4]
const arr2 = arr.map(v => v * 2)
// parameter : (조건 함수)
// arr2 [2,4,6,8]
const arr = ['Hi', 'Hello', 'Bye']
const str = arr.join()
// Hi,Hello,Bye
const str2 = arr.join('')
// HiHelloBye
const arr = [1, 2, 3, 4]
arr.push(5)
// arr [1,2,3,4,5]
const arr = [1, 2, 3, 4]
const data = arr.pop()
// arr [1,2,3]
// data 4
const arr = [1, 2, 3, 4]
arr.unshift(0)
// arr [0,1,2,3,4]
const arr = [1, 2, 3, 4]
const data = arr.shift()
// arr [2,3,4]
// data 1
;[1, 10, 101, 20].sort()
// [1, 10, 101, 20] 문자로 인식
;[1, 10, 101, 20].sort((a, b) => a - b)
// parameter : (조건식 함수)
// [1, 10, 20, 101] 숫자로 인식
\w
: 문자(character), [0-9a-zA-Z_]와 같은 문법
\W
: \w가 아닌것
\d
: 숫자, [0-9]와 같음
\D
: /d가 아닌것
\s
: 공백문자
\n
: 개행문자
.
(와일드카드)
: 모든 문자
[]
: 이 속에 있는 거 하나라도 있으면 매칭, [abc][a-z] [0-9] 등
[^]
: 해당 조건 문자 제외하고 매칭, [^abc] a랑b랑c는 모두 제외함
?
: 앞 문자가 있을 수도 없을 수도 있다
^ $
: 문자열의 처음과 끝을 의미 /m과 같이써야함 (멀티라인)
+ *
+ : 앞 문자가 1개 이상 있어야 매칭 a1 a12 a 에서 a는 매칭x
* : 앞 문자가 0개 이상 있으면 매칭 a1 a12 a 에서 a도 매칭
const str = 'A AA AAA Aa'
str.match(/AA+/g)
// ["AA", "AAA"]
str.match(/AA*/g)
// ["A", "AA", "AAA", "A"]
{n}
{n} : 앞 문자가 n개 이상있음
{n,m}, {n, } : 앞 문자가 n개 이상 (m개 이하)
()
❓ 아직이해못함 split()시 나도 포함시켜줘! 이런의미로 우선 이해함!
const str = '#감성 #감성사진 좋다! #맞팔'
str.split(/#[^\s]+/g)
// ["", " ", " 좋다! ", ""]
str.split(/(#[^\s]+)/g)
// ["", "#감성", " ", "#감성사진", " 좋다! ", "#맞팔", ""]
탐색할문자(?=조건)
/\d+(?=px)/g
뒤에 px을 빼고 숫자만 가져옴
(?<=조건)탐색할문자
(?<=\$)\d+
앞에 $를 빼고 숫자만 가져옴
/g
: 둘 이상의 일치 항목을 찾음
/i
: 대소문자 구분 없이 찾음
/m
: 주어진 문자열에 줄바꿈이 있을 경우, 여러 줄로 취급하여 찾음
const str = '#hi #hello man #bye'
str2.match(/#[^\s]+/g)
// ["#hi", "#hello", "#bye"]
String.prototype.indexOf 와 같음
const str = 'abcd'
str.search(/b/g)
// 1
배열일 경우,
Math.max(...[1, 2, 3, 6, 4])
// 6
Math.max.apply(null, [1, 2, 3, 6, 4])
// 6