问题
Continuing from RamdaJS groupBy and tranform object
let children = [
{ "name": "Bob", "age": 8, "father": "Mike" },
{ "name": "David", "age": 10, "father": "Mike" },
{ "name": "Amy", "age": 2, "father": "Mike" },
{ "name": "Jeff", "age": 11, "father": "Jack" }
]
let schoolList = [
{ "name": "Bob", "class": "8-A", "school": "School 1" },
{ "name": "David", "class": "10-B", "school": "School 1" },
{ "name": "Amy", "class": "2-A", "school": "School 1" },
{ "name": "Jeff", "class": "11-C", "school": "School 1" }
]
What have i done so far.
R.pipe(
R.groupBy(R.prop('father')),
R.map(R.applySpec({
father: R.pipe(R.head, R.prop('father')),
count: R.length,
kids: R.map(R.dissoc('father')),
class: R.map(R.prop('class'))R.find(R.propEq('name',R.pipe(R.head, R.prop('name'))), schoolList)
})),
R.values()
)(children)
Expected output is class property is appended to nested kids array.
{
"father": "Jack",
"count" : 1,
"kids": [
{ "name": "Jeff", "age": 11, "class" : "11-C" }
]
}
回答1:
If the actual schoolList is quite long, I would use the answer from OriDrori. But if it's fairly short, then this might be a bit cleaner, even if less efficient:
const expandChild = schoolList => child => ({
...child,
class: defaultTo(
{class: 'unknown'},
find(propEq('name', child.name), schoolList)
).class
})
const groupChildren = schoolList => pipe(
groupBy(prop('father')),
map(applySpec({
father: pipe(head, prop('father')),
count: length,
kids: map(pipe(dissoc('father'), expandChild(schoolList)))
})),
values,
)
let children = [{ name: "Bob", age: 8, father: "Mike" }, { name: "David", age: 10, father: "Mike" }, { name: "Amy", age: 2, father: "Mike" }, { name: "Jeff", age: 11, father: "Jack" }, { name: "Sue", age: 9, father: "Jack" }]
let schoolList = [{ name: "Bob", class: "8-A", school: "School 1" }, { name: "David", class: "10-B", school: "School 1" }, { name: "Amy", class: "2-A", school: "School 1" }, { name: "Jeff", class: "11-C", school: "School 1" }]
console.log (groupChildren (schoolList) (children))
<script src="https://bundle.run/ramda@0.26.1"></script><script>
const {defaultTo, find, propEq, pipe, groupBy, map, applySpec, head, prop, length, dissoc, values} = ramda
</script>
Note the default value given for school in case the child's name is not in schoolList.
回答2:
Convert the schoolList to a dictionary of objects by name with R.indexBy. Create pickFromLookupByName by using a flipped prop to take from the lookup. Use pick to extract the props you want.
To add the class to the kid object use R.converge that takes the kid object with R.identity, and uses pickClass to get an object that contains the class, and merges both with R.mergeRight.
const { pipe, groupBy, prop, applySpec, head, length, map, dissoc, values, indexBy, converge, mergeRight, flip, pick, identity } = R
const schoolList = [{"name":"Bob","class":"8-A","school":"School 1"},{"name":"David","class":"10-B","school":"School 1"},{"name":"Amy","class":"2-A","school":"School 1"},{"name":"Jeff","class":"11-C","school":"School 1"}]
const schoolLookup = indexBy(prop('name'), schoolList);
const pickFromLookupByName = (propList) => pipe(
flip(prop)(schoolLookup),
pick(propList),
)
const pickClass = pickFromLookupByName(['class']) // convenience method to get an object containing the class
const fn = pipe(
groupBy(prop('father')),
map(applySpec({
father: pipe(head, prop('father')),
count: length,
kids: map(pipe(
dissoc('father'),
converge(mergeRight, [identity, pipe(prop('name'), pickClass)])
))
})),
values
)
const children = [{"name":"Bob","age":8,"father":"Mike"},{"name":"David","age":10,"father":"Mike"},{"name":"Amy","age":2,"father":"Mike"},{"name":"Jeff","age":11,"father":"Jack"}]
const result = fn(children)
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script>
来源:https://stackoverflow.com/questions/55527291/ramdajs-transform-object-and-do-lookup-with-another-lists