Why is not an object properly ordered like other objects in a recursive function?

0

With this recursive function I order my objeto . The problem is that it does not order everything correctly.

Ex: the item Horn is assigned to father 269 and id 269 is inside the item Fill Syringes strong>, which is called Body . Therefore, the item Horn should be within the Body item.

I do not know why that item is not properly ordered.

Then the reordering code of my objeto .

let partes_desordenadas = [
  {
    "id": 517,
    "nombre": "Correa",
    "padre": 289,
    "conteo": 2
  },
  {
    "id": 266,
    "nombre": "Sellos",
    "padre": 262,
    "conteo": 10
  },
  {
    "id": 262,
    "nombre": "Jeringas de Llenado",
    "padre": 236,
    "conteo": 6
  },
  {
    "id": 267,
    "nombre": "Válvula Sheck",
    "padre": 262,
    "conteo": 6
  },
  {
    "id": 281,
    "nombre": "Conjunto Cabezal Magnetico",
    "padre": 239,
    "conteo": 2
  },
  {
    "id": 239,
    "nombre": "Sistema de Tapado",
    "padre": 0,
    "conteo": 2
  },
  {
    "id": 270,
    "nombre": "Bocina",
    "padre": 269,
    "conteo": 3
  },
  {
    "id": 278,
    "nombre": "O-Ring",
    "padre": 262,
    "conteo": 2
  },
  {
    "id": 236,
    "nombre": "Sistema de Llenado de Producto",
    "padre": 0,
    "conteo": 4
  },
  {
    "id": 343,
    "nombre": "Picos de Llenado",
    "padre": 262,
    "conteo": 2
  },
  {
    "id": 269,
    "nombre": "Cuerpo",
    "padre": 262,
    "conteo": 2
  },
  {
    "id": 261,
    "nombre": "Estrella Transportadora de Frasco",
    "padre": 236,
    "conteo": 1
  },
  {
    "id": 268,
    "nombre": "Regleta Dosificadora",
    "padre": 262,
    "conteo": 2
  },
  {
    "id": 265,
    "nombre": "Rotula Superior",
    "padre": 262,
    "conteo": 2
  },
  {
    "id": 264,
    "nombre": "Rotula Inferior",
    "padre": 262,
    "conteo": 2
  },
  {
    "id": 283,
    "nombre": "Dado Estriado",
    "padre": 281,
    "conteo": 1
  }
];
console.log(partes_desordenadas);

function ordenar_partes(j){
    let partes = [];
    for(n in j){
        insertar_partes(j[n], 0, partes);
    }
    return partes;
}
function insertar_partes(j, l, partes){
    for(n in partes){
        if(partes[n].id == j.padre){
            if (partes[n].partes == undefined){
                partes[n].partes = [];
            }
            return partes[n].partes.push(j);
        }else{
            if(partes[n].partes){
                if(insertar_partes(j, l+1, partes[n].partes)){
                    return true;
                }
            }
        }
    }
    if(l){
        return false;
    }
    partes.push(j);
}
var partes_ordenadas = ordenar_partes(partes_desordenadas);
console.log(partes_ordenadas);
    
asked by Pablo Contreras 06.09.2017 в 06:56
source

1 answer

0

The solution I found was to first order the òbjeto for the id padre .

let partes_desordenadas = [
  {
    "id": 517,
    "nombre": "Correa",
    "padre": 289,
    "conteo": 2
  },
  {
    "id": 266,
    "nombre": "Sellos",
    "padre": 262,
    "conteo": 10
  },
  {
    "id": 262,
    "nombre": "Jeringas de Llenado",
    "padre": 236,
    "conteo": 6
  },
  {
    "id": 267,
    "nombre": "Válvula Sheck",
    "padre": 262,
    "conteo": 6
  },
  {
    "id": 281,
    "nombre": "Conjunto Cabezal Magnetico",
    "padre": 239,
    "conteo": 2
  },
  {
    "id": 239,
    "nombre": "Sistema de Tapado",
    "padre": 0,
    "conteo": 2
  },
  {
    "id": 270,
    "nombre": "Bocina",
    "padre": 269,
    "conteo": 3
  },
  {
    "id": 278,
    "nombre": "O-Ring",
    "padre": 262,
    "conteo": 2
  },
  {
    "id": 236,
    "nombre": "Sistema de Llenado de Producto",
    "padre": 0,
    "conteo": 4
  },
  {
    "id": 343,
    "nombre": "Picos de Llenado",
    "padre": 262,
    "conteo": 2
  },
  {
    "id": 269,
    "nombre": "Cuerpo",
    "padre": 262,
    "conteo": 2
  },
  {
    "id": 261,
    "nombre": "Estrella Transportadora de Frasco",
    "padre": 236,
    "conteo": 1
  },
  {
    "id": 268,
    "nombre": "Regleta Dosificadora",
    "padre": 262,
    "conteo": 2
  },
  {
    "id": 265,
    "nombre": "Rotula Superior",
    "padre": 262,
    "conteo": 2
  },
  {
    "id": 264,
    "nombre": "Rotula Inferior",
    "padre": 262,
    "conteo": 2
  },
  {
    "id": 283,
    "nombre": "Dado Estriado",
    "padre": 281,
    "conteo": 1
  }
];

partes_desordenadas.sort(function(a, b){
    let keyA = a.padre,
        keyB = b.padre;
    // Compare the 2 dates
    if(keyA < keyB) return -1;
    if(keyA > keyB) return 1;
    return 0;
});

function ordenar_partes(j){
    let partes = [];
    for(n in j){
        insertar_partes(j[n], 0, partes);
    }
    return partes;
}
function insertar_partes(j, l, partes){
    for(n in partes){
        if(partes[n].id == j.padre){
            if (partes[n].partes == undefined){
                partes[n].partes = [];
            }
            return partes[n].partes.push(j);
        }else{
            if(partes[n].partes){
                if(insertar_partes(j, l+1, partes[n].partes)){
                    return true;
                }
            }
        }
    }
    if(l){
        return false;
    }
    partes.push(j);
}
let partes_ordenadas = ordenar_partes(partes_desordenadas);
console.log(partes_ordenadas);
    
answered by 06.09.2017 / 22:58
source