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

151
Visualizações
Como resolver mis necesidades con reduce slove mi pregunta
const formatData = (data) => { return data.map((item) => { let news = { ...item, isLeaf: item.haveNext ? false : true } if (item.children && item.children.length > 0) { news.children = formatData(item.children); } return news }) } let arr = [{ title: 1, haveNext: false, children: [{ title: 2, haveNext: true, children: [{ title: 3, haveNext: true, }] }] }]

quiero arr cambiar a
No sé cómo escribir algo de código. Por favor, ayúdenme arr2 = [{ title: 1, isLeaf: false, children: [{ title: 2, isLeaf: true, children: [{ title: 3, isLeaf: true, }] }] }]

 const formatData2 = (data) => { return data.reduce((acc, { haveNext, ...rest }) => { let news:any = { ...rest, isLeaf: haveNext ? false : true } if (rest.children && rest.children.length > 0) { //How to write here } acc = acc.concat(news) return acc }, []) } console.log(formatData2(arr))
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Puede usar una función recursiva para obtener los resultados que necesita.

 let arr = [{ title: 1, haveNext: false, children: [{ title: 2, haveNext: true, children: [{ title: 3, haveNext: true, }] }] }]; function loop(arr) { // `map` over the array return arr.map(obj => { // Destructure the properties from the each array object const { children, haveNext, title } = obj; // If there is a "children" property create a // new object but call the function again with the children // and change the haveNext prop to newLeaf if (children) { return { title, newLeaf: haveNext, children: loop(children) }; } // Otherwise just return the updated object // (haveNext to newLeaf) return { title, newLeaf: haveNext }; }); } console.log(loop(arr));

Documentación adicional

  • map

  • Asignación de desestructuración

about 4 years ago · Juan Pablo Isaza Relatório

0

Esta es una versión reduce , pero en realidad no reduce nada. Array.map es una mejor opción para este caso de uso.

Por cierto, su isLeaf y haveNext son literalmente incorrectos, sugiero que verifique a los children en su lugar.

Documentación Array.reduce zh-CN

 const formatData = (data) => data.reduce((acc, {children, ...item}) => { let news = { ...item }; if (children && children.length > 0) { news.children = formatData(children); news.isLeaf = children.length === 0; } else { news.isLeaf = true; } acc.push(news); return acc; }, []) const arr = [{ title: 1, haveNext: false, children: [{ title: 2, haveNext: true, children: [{ title: 3, haveNext: true, }] }] }] console.log(formatData(arr))

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