[Transducer] Transduce When the Collection Type is an Object
2018-01-17 20:44
513 查看
We've seen how we can
To do this we'll be using a lodash function called entries.
The whole point to make collection works for Object type is because when we use for.. of loop, Object is not itertable type, so Object still cannot be used. The fix that problem, we can use 'entries' from lodash, to only get value as an array from the Object, so that we can loop though the array.
transducefrom arrays or other iterables, but plain objects aren't iterable in Javascript. In this lesson we'll modify our
transduce()function so that it supports iterating from plain objects as well, treating each key value pair as an entry in the collection.
To do this we'll be using a lodash function called entries.
The whole point to make collection works for Object type is because when we use for.. of loop, Object is not itertable type, so Object still cannot be used. The fix that problem, we can use 'entries' from lodash, to only get value as an array from the Object, so that we can loop though the array.
import {isPlainObject, entries} from 'lodash'; import {map, into} from '../utils'; let transduce = (xf /** could be composed **/, reducer, seed, _collection) => { const transformedReducer = xf(reducer); let accumulation = seed; const collection = isPlainObject(_collection) ? entries(_collection) : _collection; for (let value of collection) { accumulation = transformedReducer(accumulation, value); } return accumulation; }; const objectValues = obj => { return into([], map(kv => kv[1]), obj); }; objectValues({one: 1, two: 2});
相关文章推荐
- The type 'System.Data.Objects.DataClasses.EntityObject' is defined in an assembly that is not refere
- How do I check if a type is a subtype OR the type of an object?
- collection element of type 'CGColorRef _Nullable' (aka'stuct CGColor*')is not an Objective-C object
- Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
- Java出现No enclosing instance of type H is accessible. Must qualify the allocation with an enclosing
- 出现No enclosing instance of type Exp is accessible. Must qualify the allocation with an enclosing ins
- The type java.lang.Object cannot be resolved It is indirectly referenced ...
- Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
- 【java学习之旅】—the type java.io.ObjectInputStream cannot be resolved. It is indirectly......
- Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
- 错误提示:No enclosing instance of type A is accessible. Must qualify the allocation with an enclosing
- The type java.io.ObjectInputStream cannot be resolved. It is indirectly referenced from required .cl
- Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
- No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing insta
- Error:You must write an attribute 'type'='object' after writing the attribute with local name '__type'
- Eclipse中出现错误“void is an invalid type for the variable xxx”
- Java错误No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
- Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
- Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
- No enclosing instance of type E is accessible. Must qualify the allocation with an en