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

138
Vistas
reestructurar la posición de los elementos de la matriz

Tengo una matriz llamada myArray con objetos como elementos. Lo que estoy tratando de lograr es reestructurar la posición de los elementos de la matriz sin tener dos objetos con la misma clave uno al lado del otro. El método que estoy usando en este momento comienza a funcionar pero se estropea después de ir a la mitad. ¿Cómo puedo conseguir esto? Gracias por adelantado.

lo que estoy tratando de lograr: finalArray = [{ "mañana": "..." }, { "tarde": "..." }, { "mañana": "..." }, { "tarde ": "..." }, { "mañana": "..." }, { "tarde": "..." }, { "mañana": "..." }, { "tarde": "..." }, { "tarde": "..." }, { "tarde": "..." }]

 const myArray = [{ "morning": "23_6557" }, { "evening": "7665_908" }, { "evening": "545_787" }, { "evening": "8774_957" }, { "morning": "434_6447" }, { "morning": "775_67" }, { "evening": "4554_8774" }, { "evening": "224_74" }, { "evening": "43_112" }, { "morning": "32_47" }] orderedArray = [] unOrderedArray = [] myArray.forEach((obj) => { //if the orderedArray is empty push a random array to it if (orderedArray.length == 0) { orderedArray.push(obj) } Object.keys(obj).forEach((key) => { //get the last element in the orderedArray and check if its key is equal to the new obj key if (Object.keys(orderedArray.at(-1))[0] != key) { //get a random object from myArray with key not equal to last orderedArray object key and push it to orderedArray orderedArray.push(obj) } else { //get a random object from myArray with key equal to last orderedArray object key and push it to unOrderedArray unOrderedArray.push(obj) } }); }); const finalArray = orderedArray.concat(unOrderedArray) console.log(finalArray)

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Una forma es agrupar primero los datos por propiedad y luego alternar entre los grupos para extraer un elemento:

 const myArray = [{"morning": "23_6557"}, {"evening": "7665_908"}, {"evening": "545_787"}, {"evening": "8774_957"}, {"morning": "434_6447"}, {"morning": "775_67"}, {"evening": "4554_8774"}, {"evening": "224_74"}, {"evening": "43_112"}, {"morning": "32_47"}]; // Create a map keyed by the properties const map = new Map(myArray.map(o => [Object.keys(o)[0], []])); // Populate the arrays that are associated by those properties for (const o of myArray) map.get(Object.keys(o)[0]).push(o); // Extract the arrays from this map const groups = [...map.values()]; // Alternate between the groups to pull objects from them const result = []; while (groups.length) { const group = groups.shift(); result.push(group.shift()); if (group.length) groups.push(group); } console.log(result);

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