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

176
Vistas
How to transform and reorder array in JavaScript?

Is there an elegant way to reorder JavaScript array? For example, says I have an array like this:

let array = ['a', 'b', 'c']

Now I want to rearrange this to

['b', 'a', 'a', 'c', 'a']

Is there a built-in higher-order function to do that in JavaScript? Something like

let reordered = array.reorderMagic([1, 0, 0, 2, 0])

Where I could just specify an index array? If not, can you suggest a library that can help me do these kinds of matrix-like manipulations?

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

0

You can create a method on Array.prototype though It is not recommended

Read: JavaScript: What dangers are in extending Array.prototype?

if (!('reorderMagic' in Array.prototype)) {
  Array.prototype.reorderMagic = function(ref) {
    const arr = this;
    return ref.map((n) => arr[n]);
  };
}

let array = ["a", "b", "c"];
let reordered = array.reorderMagic([1, 0, 0, 2, 0]);
console.log(reordered);

But I won't recommend that because there might be a scenario where someone or any library might override your method(This is just one scenario)

So you can just make a utility function and get the desired result as:

function reorderMagic(arr, ref) {
  return ref.map((n) => arr[n]);
}

let array = ["a", "b", "c"];
let reordered = reorderMagic(array, [1, 0, 0, 2, 0]);
console.log(reordered);

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can create a method on Array.prototype

Extending Array.prototype is considered bad practice, but the rest of the solution does work well.

I would recommend an API something like this instead:

function chooseIndices(arr, indexes) {
  return indexes.map(i => arr[i]);
}

And then use it as such:

let array = ["a", "b", "c"];
let reordered = chooseIndices(array, [1, 0, 0, 2, 0]);
// reordered === ['b', 'a', 'a', 'c', 'a']

Caution:

The function above doesn't account for invalid indices. For example, if you use the index 5 in the example above, you'll just get an undefined for it.

You can decide how you want to handle invalid indices. But one option would be to simply filter those out before using the index array.

about 4 years ago · Juan Pablo Isaza Denunciar

0

const arr = ['a','b','c'];
const order = [1, 0, 0, 2, 0];

const reorderMagic = (arr, indexes) => indexes.map((i) => arr[i]);

console.log(reorderMagic(arr, order));
// [ "b","a","a","c","a"]

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