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

210
Visualizações
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 Respostas
Responde à pergunta

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