函数式编程
filter的使用
var animals = [
{name:'Fluffykins',species:'rabbit'},
{name:'Caro',species:'dog'},
{name:'Hamilton',species:'dog'},
{name:'Harold',species:'fish'},
{name:'Ursula',species:'cat'},
{name:'Jimmy',species:'fish'}
]
//第一种做法
var dogs = []
for (var i = 0; i < animals.length; i++){
if(animals[i].species === 'dog'){
dog.push(animals[i])
}
}
//第二种做法
var dogs = animals.filter(function(animal){
return animal.species === 'dog'
})
//优化
let isDog = unction(animal){
return animal.species === 'dog'
}
var dogs = animals.filter(isDog)
map的使用
var animals = [
{name:'Fluffykins',species:'rabbit'},
{name:'Caro',species:'dog'},
{name:'Hamilton',species:'dog'},
{name:'Harold',species:'fish'},
{name:'Ursula',species:'cat'},
{name:'Jimmy',species:'fish'}
]
//第一种做法
var names = []
for ( var i = 0 ; i < animals.length; i++){
names.push(animals[i].name)
}
//第二种做法
var names = animals.map(function(animal){
return animal.name
})
//优化
var names = animals.map((animal) => animal.name)
reduce
var orders = [
{amount:250},
{amount:400},
{amount:100},
{amount:325}
]
//第一种做法
var totalAmount = 0
for ( var i = 0; i < orders.length; i++){
totalAmount = orders[i].amount
}
//第二种做法
var totalAmount = orders.reduce(function(sum,order){
return sum + order.amount
},0)
//优化
var totalAmount = orders.reduce((sum,order) => sum + order.amount
},0)
curry
let dragon = (name,size,element) =>
`${name} is a ${size} dragon that breathes ${element} !`
console.log(dragon('fluffykins','tiny','lightling'))
//curry
let dragon =
name =>
size =>
element =>
`${name} is a ${size} dragon that breathes ${element} !`
console.log(dragon('fluffykins')('tiny')('lightling'))
//还可以
let dragon =
name =>
size =>
element =>
`${name} is a ${size} dragon that breathes ${element} !`
let fluffykinDragon = dragon('fluffykins')
console.log(fluffykinDragon('tiny')('lightling'))
//甚至是这样
let dragon =
name =>
size =>
element =>
`${name} is a ${size} dragon that breathes ${element} !`
let fluffykinDragon = dragon('fluffykins')
let tinyDragon = fluffykinDragon('tiny')
console.log(tinyDragon('lightling'))
//使用lodash
import _ from 'lodash'
let dragon =(name,size,element) => (
`${name} is a ${size} dragon that breathes ${element} !`
)
dragon = _.curry(dragon)
let fluffykinDragon = dragon('fluffykins')
let tinyDragon = fluffykinDragon('tiny')
console.log(tinyDragon('lightling'))
let dragons = [
{name:'fluffykins',element:'lightning'},
{name:'nomi',element:'lightning'},
{name:'karo',element:'fire'},
{name:'doomer',element:'timewrap'},
]
let hasElement = (element,obj) => obj.element === element
let lightingDragons = dragons.filter(x => hasElement('lightning',x)
console.log(lightingDragons)
//使用loadash_curry改造
import _ from 'lodash'
let hasElement = _.curry(element,obj) => obj.element === element
let lightingDragons = dragons.filter(hasElement('lightning'))
console.log(lightingDragons)
递归
let categories = [
{
id:'animals',
'parent':null
},
{
id:'mammals',
'parent':'animals'
},
{
id:'cats',
'parent':'mammals'
},
{
id:'dogs',
'parent':'mammals'
},
{
id:'chihuahua',
'parent':'dogs'
},
{
id:'labrador',
'parent':'dogs'
},
{
id:'persian',
'parent':'cats'
},
{
id:'siamese',
'parent':'cats'
}
]
let makeTree = (categories,parent) => {
let node = {
}
categories.filter(c => c.parent === parent)
.forEach(c =>
node[c.id] = makeTree(categories,c.id))
return node
}
console.log(
JSON.stringify(
makeTree(categories,null)
,null
,2
)
)