Created
June 7, 2018 17:50
-
-
Save peekg/80327633e2b21aa3129e8421f780f3c2 to your computer and use it in GitHub Desktop.
immutable.js iterate map
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//@flow | |
type Props = { | |
name: string | |
} | |
type MyRecord = RecordOf<Props> | |
type MyMap = Map<MyRecord,List<number>> | |
const create: MyRecordFactory<Props> = Record({name:""}); | |
const myMap = Map().set(create({name:"hi"}), List([1]) ) | |
//example 1 | |
const arr = Seq(myMap.entries()).reduce( (list, map) => { | |
list.push( map[0].name ) | |
return list; | |
}, [] ); | |
//example 2 | |
const iterMap = ( iter, cb ) => { | |
const list = []; | |
const func = (iter, cb) => { | |
const n = iter.next(); | |
return n.done ? list : list.push( cb( n.value ) ) && func(iter,cb); | |
} | |
return func( iter, cb ); | |
} | |
const arr2 = iterMap( myMap.keys(), it => it.name ) | |
arr.concat(arr2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment