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

116
Visualizações
Cómo reordenar una matriz de objetos usando otra matriz que tiene índice y devuelve la nueva matriz ordenada

Lo que quiero exactamente es una función en la que podamos pasar dos matrices, una es la matriz original de objetos con un orden específico y la otra es una matriz de objetos que tiene el nuevo índice de destino para que la función devuelva la matriz original al nuevo orden basado en en los índices

Por ejemplo: así es como se verá la matriz original

 const columns=[ { field: "name", type: "action", width: settings.defaultColumnWidth, }, { field: "country", type: "action", width: settings.defaultColumnWidth, }, { field: "phone", type: "action", width: settings.defaultColumnWidth, }, { field: "email", type: "action", width: settings.defaultColumnWidth, }, ]

Y la otra matriz que tiene objetos con nombres de campos y el índice de destino para ordenar

 const newOrder = [ { field: "country" oldIndex: 1 targetIndex: 2 }, ]

Entonces, la nueva matriz que debe devolverse de la función será así

 const columns=[ { field: "name", type: "action", width: settings.defaultColumnWidth, }, { field: "phone", type: "action", width: settings.defaultColumnWidth, }, { field: "country", type: "action", width: settings.defaultColumnWidth, }, { field: "email", type: "action", width: settings.defaultColumnWidth, }, ]

Espero que mi pregunta sea clara, gracias de antemano por su ayuda.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Puede lograr esto con la técnica de intercambio.

 var settings = { defaultColumnWidth: 6 } const columns=[ { field: "name", type: "action", width: settings.defaultColumnWidth, }, { field: "country", type: "action", width: settings.defaultColumnWidth, }, { field: "phone", type: "action", width: settings.defaultColumnWidth, }, { field: "email", type: "action", width: settings.defaultColumnWidth, }, ]; const newOrder = [ { field: "country", oldIndex: 1, targetIndex: 2 }, ]; newOrder.forEach(function(order) { // swapping let temp = columns[ order['targetIndex'] ]; columns[ order['targetIndex'] ] = columns[ order['oldIndex'] ]; columns[ order['oldIndex'] ] = temp; }); console.log( columns );

about 4 years ago · Juan Pablo Isaza Relatório

0

Suponiendo que no habrá demasiada contradicción, creo que el splice y demás deberían funcionar.

 var settings = { defaultColumnWidth: 12 } const columns = [{ field: "name", type: "action", width: settings.defaultColumnWidth }, { field: "country", type: "action", width: settings.defaultColumnWidth }, { field: "phone", type: "action", width: settings.defaultColumnWidth }, { field: "email", type: "action", width: settings.defaultColumnWidth } ] const newOrder = [{ field: "country", oldIndex: 1, targetIndex: 2 } ]; newOrder.forEach(function(item) { var field = item.field var targetIndex = item.targetIndex for (var i = 0; i < columns.length; i++) { var column = columns[i]; if (column.field == field) { var elem = columns.splice(i, 1)[0]; columns.splice(targetIndex, 0, elem); break; } } }) console.log(columns);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Relatório

0

Aquí hay una versión simple

 Array.prototype.move = function(from, to) { this.splice(to, 0, this.splice(from, 1)[0]);}; const columns = [{ field: "name", type: "action", width: "settings.defaultColumnWidth", }, { field: "country", type: "action", width: "settings.defaultColumnWidth", }, { field: "phone", type: "action", width: "settings.defaultColumnWidth", }, { field: "email", type: "action", width: "settings.defaultColumnWidth", }, ]; const newOrder = [{ field: "country", oldIndex: 1, targetIndex: 2 } ]; newOrder.forEach(({ field, oldIndex, targetIndex }) => columns[oldIndex].field === field && columns.move(oldIndex,targetIndex)); console.log(columns)

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