Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

150
Vistas
How to solve my needs with reduce slove my question
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,
        }]
    }]
}]

i want to arr change to
I don't know how to write some code. Please help me 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 Respuestas
Responde la pregunta

0

You can use a recursive function to get the results you need.

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));

Additional documentation

  • map

  • Destructuring assignment

about 4 years ago · Juan Pablo Isaza Denunciar

0

This is reduce version, but it actually does not reduce anything. Array.map is better choice for this use case.

Btw, your isLeaf & haveNext are literally incorrect, I suggest to check children instead.

Documentation 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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda