Array reduce

Array reduce

API

1
2
arr.reduce([callback,initialValue])
  • callback(prev,current,currentIndex,array)
  • initialValue 可选,第一个参数
  • 拍平数组
1
2
3
4
5
var testArr1 = [[0, 1], [2, 3], [4, 5]];
var testArr2 = [0, [1, [2, [3, [4, [5]]]]]];
flatten(testArr1) // [0, 1, 2, 3, 4, 5]
flatten(testArr2) // [0, 1, 2, 3, 4, 5]
1
2
3
4
5
flatten = arr=>{
return arr.reduce((prev,current)=>{
return Array.isArray(current) ? prev.concat(flatten(current)) : prev.concat(current)
},[])
}
  • 获取深层数据结构中值
1
2
3
4
5
6
7
8
9
10
11
12
const props = {
user: {
posts: [
{ title: 'Foo', comments: [ 'Good one!', 'Interesting...' ] },
{ title: 'Bar', comments: [ 'Ok' ] },
{ title: 'Baz', comments: [] },
]
}
}
const getUserComments = get(['user', 'posts', 0, 'comments'])
getUserComments(props) // [ 'Good one!', 'Interesting...' ]
1
2
//ramda compose
const get = p=> o => p.reduce((xs,x)=>(xs && xs[x] ? xs[x] : undefined),o)