const arr = [1, 2, 0, -1, -2]
const res = arr.some(key => key < 0)
console.log(res)
arr.forEach(item => console.log(item))
9. filter 와 map 함수 사용
-map은 각각의 변수에 조건을 넣어 주고
- filter는 각각에 맞는 값들만 배열로 생성
const a = [1,2,3]
const b = a.map(x => x + 1)
console.log(b)
const f = a.filter(x => x > 1)
console.log(f)
>>> 결과값
[ 2, 3, 4 ]
[ 2, 3 ]
10. object assign 과 Spread
음.. Spread가 훨씬 유익한 것 같은데
'use strict'
const obj = {
title : 'node.js 올인원 패키지'
}
const newObj = {
name : 'node.js 온라인'
}
const ret = Object.assign({}, obj, newObj)
//{새로운 객체를 만들고}, 합칠 오브젝트 1, 추가 오브젝트 2
console.log(ret)
//객체와 배열 모두 하나로 통합할 수 있다.
const ret2 = {
...obj,
...newObj,
}
console.log(ret2)
var arr = [1,2,3]
var arr2 = [4,5,6,7,3]
const ret_arr = Object.assign({},arr,arr2)
console.log(ret_arr)
const res_arr = [
...arr,
...arr2
]
console.log(res_arr)
>>>>{ title: 'node.js 올인원 패키지', name: 'node.js 온라인' }
{ title: 'node.js 올인원 패키지', name: 'node.js 온라인' }
{ '0': 4, '1': 5, '2': 6, '3': 7, '4': 3 }
[
1, 2, 3, 4,
5, 6, 7, 3
]
11. Set
반복되는 데이터를 정제하는 문법
const test= new Set()
test.add(1)
test.add(2)
test.add(1)
test.add(2)
test.add(3)
test.add(2)
console.log(test)
const arr = []
for(const item of test){
arr.push(item)
}
console.log(arr)
const ret = test.has(1)
console.log(ret)
>>>
Set { 1, 2, 3 }
[ 1, 2, 3 ]
true
12. template String
정말 유용한 기능중에 하나 -> 기존에 변수를 추가하여 사용하고 싶을때 사용하는 방법임
const details = `자세한 내용`
let str = `node.js`
str += `올인원 패키지 ${details}`
const int =1
str += `${str}의 값은${int}`
console.log`입력`
13. String
문자열에대한 검사 -> 사실 Java에서는 문자열 검사가 상당히 번거로웠지만, javascript는 기본적으로 다 된다.
let string = 'node.js 올인원 패키지'
let isStartWith = string.startsWith('n')
let isIncludes = string.includes('올인원')
let isEndWith = string.endsWith('패키지')
const checkIfContainer = () => {
if (isEndWith && isStartWith && isEndWith){
return true
}
}
const ret = checkIfContainer()
console.log(ret)
14. typeof 로 타입을 확인 할 수 있다.
15. hoisting : 함수가 나중에 호출되어도 가능하다
16. IIFE 즉시 실행함수
익명함수 (function (){} )();
var r = (function(){
var lang = 'js';
return lang
})();
console.log(r)
17. SetInterval
setInterval(() => {
console.log('hi')
},1000)
18. try catch
try {
}catch(e){
}
19. Arrorw function
const add = (var1, var2) => (var1 + var2)
console.log(add(1,2))
const getDiscount = rate => price => rate * price
const getTenpercentOff = getDiscount(0.1)
console.log(getTenpercentOff(1000))