Published
Edited
Sep 1, 2018
3 stars
Insert cell
Insert cell
collection = [{id: 'a', name: 'A', number: 1}, {id: 'b', name: 'B', number: 2}]
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
collection.reduce((acc, x) => ({...acc, [x.id]: x}), {})
Insert cell
Insert cell
collection.reduce((acc, x) => ({...acc, [x.id]: {name: x.name, number: x.number}}), {})
Insert cell
Insert cell
fields = ['name', 'number']
Insert cell
Insert cell
objFilter = (keys, obj) => {
return Object.keys(obj).reduce((acc, x) => {
return keys.includes(x) ? {...acc, [x]: obj[x]} : acc
}, {})
}
Insert cell
collection.reduce((acc, x) => ({...acc, [x.id]: objFilter(fields, x)}), {})
Insert cell
Insert cell
collection.reduce((acc, x) => ({...acc, [x.id]: R.pick(fields, x)}), {})
Insert cell
Insert cell
Insert cell
Insert cell
reducer = (acc, x) => ({...acc, [x.id]: R.pick(fields, x)})
Insert cell
collection.reduce(reducer, {})
Insert cell
Insert cell
configurableReducer = (k, fields) => (acc, x) => ({...acc, [x[k]]: R.pick(fields, x)})
Insert cell
collection.reduce(configurableReducer('id', fields), {})
Insert cell
Insert cell
Insert cell
Insert cell
reduceObjWith = (fn) => (acc, x) => ({...acc, ...fn(x)})
Insert cell
Insert cell
transformer = x => ({[x.id]: objFilter(fields, x)})
Insert cell
Insert cell
collection.reduce(reduceObjWith(transformer), {})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
R.zipObj(['a', 'b'], [{name: 'A', number: 1}, {name: 'B', number: 2}])
Insert cell
Insert cell
R.zipObj(R.pluck('id', collection), R.map(R.pick(fields), collection))
Insert cell
Insert cell
R.converge(R.zipObj, [R.pluck('id'), R.map(R.pick(fields))])(collection)
Insert cell
Insert cell
Insert cell
R.map(n => R.pick(fields, R.head(n)), R.groupBy(R.prop('id'), collection))
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more