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

201
Vistas
sorting comments data algorithm with php or javascript
const comments = [
    { 'id': 1, 'origin_id': 0 },
    { 'id': 2, 'origin_id': 0 },
    { 'id': 3, 'origin_id': 2 },
    { 'id': 4, 'origin_id': 2 },
    { 'id': 5, 'origin_id': 3 },
    { 'id': 6, 'origin_id': 3 },
    { 'id': 7, 'origin_id': 0 },
    { 'id': 8, 'origin_id': 0 },
    { 'id': 9, 'origin_id': 0 },
    { 'id': 10, 'origin_id': 0 },
];

// or? data structure somthing like this? i dont konw how to use such order and depth.

const comments = [
    { 'id': 1, 'origin_id': 0, 'reply_order': 0, 'reply_depth': 0 },
    { 'id': 2, 'origin_id': 0, 'reply_order': 0, 'reply_depth': 0 },
    { 'id': 3, 'origin_id': 2, 'reply_order': 0, 'reply_depth': 0 },
    { 'id': 4, 'origin_id': 2, 'reply_order': 0, 'reply_depth': 0 },
    { 'id': 5, 'origin_id': 3, 'reply_order': 0, 'reply_depth': 0 },
    { 'id': 6, 'origin_id': 3, 'reply_order': 0, 'reply_depth': 0 },
    { 'id': 7, 'origin_id': 0, 'reply_order': 0, 'reply_depth': 0 },
    { 'id': 8, 'origin_id': 0, 'reply_order': 0, 'reply_depth': 0 },
    { 'id': 9, 'origin_id': 0, 'reply_order': 0, 'reply_depth': 0 },
    { 'id': 10, 'origin_id': 0, 'reply_order': 0, 'reply_depth': 0 },
];

i want some algorithm something like comments in comment. is that even possible with only that amount of data?

if someone knows any better way to implements that goals, please, tell me.

i want to concept of this. any code lang would be OK with me. but i prefer PHP and JS.

const comments = [
    { 'id': 1, 'origin_id': 0 },
    { 'id': 2, 'origin_id': 0 },
    { 'id': 3, 'origin_id': 2 },
    { 'id': 4, 'origin_id': 2 },
    { 'id': 5, 'origin_id': 3 },
    { 'id': 6, 'origin_id': 3 },
];

// result i expected;
const result_comments = [
    { 'id': 1, 'origin_id': 0 },
    {
        'id': 2, 'origin_id': 0,
        'children': [{
            'id': 3, 'origin_id': 2, 'children': [{ 'id': 5, 'origin_id': 3 },
            { 'id': 6, 'origin_id': 3 },]
        }, { 'id': 4, 'origin_id': 2 }]
    },
];
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The nested object here is basically like a tree and comments become tree nodes. first, create a dictionary with children's property and then assign the children from it.

comments id are created in ascending order, so you don't need to re-sort them after creating.

// assuming that id are sorted, else you have to sort comments by id.
const comments = [
    { 'id': 1, 'origin_id': 0 },
    { 'id': 2, 'origin_id': 0 },
    { 'id': 3, 'origin_id': 2 },
    { 'id': 4, 'origin_id': 2 },
    { 'id': 5, 'origin_id': 3 },
    { 'id': 6, 'origin_id': 3 },
];

// creating dictinary where key is id and value is object with added children property
let dic = comments.reduce((acc, obj) => (acc[obj.id] = {...obj, children : [] }, acc), {});

// initial set 
let treeRoot = {'id': 0, 'origin_id': 0 , children : []};

// adding in dictionary so i can add origin_id 0 to tree root
dic[treeRoot.id] = treeRoot;

// populating the childrens
comments.forEach(obj => dic[obj.origin_id].children.push(dic[obj.id]));

// if you want the array that inside treeRoot.children
console.log(treeRoot);

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