const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const result = words.filter(word => word.length > 6);
console.log(result);
// expected output: Array ["exuberant", "destruction", "present"]
arr.filter(callback(element[, index[, array]])[, thisArg])
const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
function callback(element) {
// console.log(element)
if(element.length > 6) {
return true
} else {
return false
}
}
newWords = words.filter(callback)
console.log(newWords) // ['exuberant', 'destruction', 'present']
const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
function callback(element) {
// 간소화
return element.length > 6
}
newWords = words.filter(callback)
console.log(newWords) // ['exuberant', 'destruction', 'present']
const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
// 콜백함수 자체를 인자로
newWords = words.filter(element => element.length > 6)
console.log(newWords) // ['exuberant', 'destruction', 'present']