Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

154
Visualizações
Ramda - how to get nested array values

I am trying to get values from nested arrays using Ramda. I have multiple groups like in the example below. I need to get all children from all sections and all childrenWithoutSections in one array of strings.

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"]
   }
]

I started with something like this:

R.pipe(
  R.pluck(['childrenWithoutSections']),
  R.flatten
)(groups)

And as a result, I got all the children from one required key but I have no idea how to get nested values from sections/children?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Another option is to use R.juxt to get children from the sections, and the childrenWithoutSections and then flattening the results. By chaining the results we get the array of values.

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>

about 4 years ago · Juan Pablo Isaza Relatório

0

As well as the suggestions in the comments, we can also write a point-free version of this:

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>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda