map()
μ μ½λ°±ν¨μκ° μ μ©λ μ λ°°μ΄ returnfilter()
μ 쑰건문μ λ§μ‘±ν μμλ€μ μλ‘μ΄ λ°°μ΄λ‘ returnλ°°μ΄μ μννλ©° μμλ§λ€ callback ν¨μ μ μ© ν μλ‘μ΄ λ°°μ΄λ‘ 리ν΄
λ°°μ΄μ μννλ©° μμλ§λ€ 쑰건 νμΈ ν 쑰건μ λ§μ‘±νλ μμλ€λ‘ ꡬμ±λ μλ‘μ΄ λ°°μ΄ λ¦¬ν΄
β μ§μ λ ν¨μμ κ²°κ³Ό κ°μ true
λ‘ λ§λλ μμλ€λ‘λ§ κ΅¬μ±λ μλ‘μ΄ λ°°μ΄μ λ°ν
μμ 1 // (1) map let numbers = [1, 2, 3, 4, 5] let new_num = numbers.map(function(num) { return num * 2 }) console.log(numbers) // [1, 2, 3, 4, 5] console.log(new_num) // [ 2, 4, 6, 8, 10 ] // (2) filter let numbers = [1, 2, 3, 4, 5] let new_num = numbers.filter(number => number % 2 === 0) console.log(numbers) // [1, 2, 3, 4, 5] console.log(new_num) // [ 2, 4 ] β κΈ°μ‘΄ λ°°μ΄μμ filteringμ΄ νμν 쑰건μ κ±Έμ΄μ€λ€
μμ 2 let users = [ { id:1, name:'id', age:36 }, { id:2, name:'ds', age:32 }, { id:3, name:'dw', age:32 }, { id:4, name:'xc', age:37 }, { id:5, name:'we', age:25 }, { id:6, name:'th', age:26 }, { id:7, name:'gh', age:31 }, { id:8, name:'jh', age:23 }, ] // 1) 30μΈ μ΄μμΈ usersλ₯Ό κ±°λ₯Έλ€ let over_30 = users.filter( user => user.age >= 30 ) //console.log(over_30) // [ // { id: 1, name: 'id', age: 36 }, // { id: 2, name: 'ds', age: 32 }, // { id: 3, name: 'dw', age: 32 }, // { id: 4, name: 'xc', age: 37 }, // { id: 7, name: 'gh', age: 31 } // ] // 2) 30μΈ μ΄μμΈ usersμ nameμ μΆλ ₯ let over_30 = user.filter( user => user.age >= 30 ) let mapping = over_30.map(function(el) { return el.name }) // console.log(mapping) // [ 'id', 'ds', 'dw', 'xc', 'gh' ]