Estoy tratando de obtener valores de matrices anidadas usando Ramda. Tengo varios grupos como en el siguiente ejemplo. Necesito obtener todos los niños de todas las sections y todos los childrenWithoutSections sin secciones en una matriz de cadenas.
const groups = [ { "id":"10", "sections":[ { "id":"1", "children":["10", "11"] }, { "id":"2", "children":["12"] } ], "childrenWithoutSections":["1", "2"] }, { "id":"11", "sections":[ { "id":"3", "children":["13", "14"] }, { "id":"4", "children":["15"] } ], "childrenWithoutSections":["3", "4"] } ]Empecé con algo como esto:
R.pipe( R.pluck(['childrenWithoutSections']), R.flatten )(groups) Y como resultado, obtuve todos los elementos secundarios de una clave requerida, pero no tengo idea de cómo obtener valores anidados de sections/children .
Otra opción es usar R.juxt para obtener children de las sections y childrenWithoutSections sin secciones y luego aplanar los resultados. Al encadenar los resultados obtenemos la matriz de valores.
const {chain, pipe, juxt, prop, pluck, flatten } = R const fn = chain(pipe( juxt([ pipe(prop('sections'), pluck('children')), prop('childrenWithoutSections') ]), flatten, )) const groups = [{id: "10", sections: [{id: "1", children: ["10", "11"]}, {id: "2", children: ["12"]}], childrenWithoutSections: ["1", "2"]}, {id: "11", sections: [{id: "3", children: ["13", "14"]}, {id: "4", children: ["15"]}], childrenWithoutSections: ["3", "4"]}] const result = fn(groups) console.log(result) .as-console-wrapper {max-height: 100% !important; top: 0} <script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.27.2/ramda.min.js"></script>Además de las sugerencias en los comentarios, también podemos escribir una versión sin puntos de esto:
const extract = chain ( lift (concat) ( pipe (prop ('sections'), pluck ('children'), flatten), prop ('childrenWithoutSections') ) ) const groups = [{id: "10", sections: [{id: "1", children: ["10", "11"]}, {id: "2", children: ["12"]}], childrenWithoutSections: ["1", "2"]}, {id: "11", sections: [{id: "3", children: ["13", "14"]}, {id: "4", children: ["15"]}], childrenWithoutSections: ["3", "4"]}] console .log (extract (groups)) .as-console-wrapper {max-height: 100% !important; top: 0} <script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.27.2/ramda.min.js"></script> <script> const {chain, lift, concat, pipe, prop, pluck, flatten} = R </script>