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

325
Vistas
Extract subarray of Columns from a multidimensional array and mutate the original array to remove those columns JavaScript

I want to extract a subarray of Columns from a multidimensional array and mutate the original array to remove those columns in JavaScript

Ex: If I have an array

originalArray =
[
 [A, B, C, D, E, F],
 [a1,b1,c1,d1,e1,f1],
 [a2,b2,c2,d2,e2,f2],
 [a3,b3,c3,d3,e3,f3]
[  

Where A,B,C,D,E,F are headers and I want to extract columns [4,0,3] in this order I would get

subArray =
[
 [E, A, D],
 [e1,a1,d1],
 [e2,a2,d2],
 [e3,a3,d3]
[


originalArray = 
[
 [B, C, F],
 [b1,c1,f1],
 [b2,c2,f2],
 [b3,c3,f3]
[  

I found this: Extract subarray from a multidimensional array and mutate the original array

which does this for rows

let subArray = originalArray.reduce((accumulator, current, index) => {
    if (idxList.includes(index)) {
      //if the current index is included in the idxList array, 
      //push the current value to the accumulator
      accumulator.push(current);
    } else {
      //push the current value to the `tempArray`.
      tempArray.push(current)
    }
    return accumulator;
  }, []);

Then I will concatenate these two arrays

reorderedArray = [...subArray, ...originalArray]

Reduce seems like an interesting approach and it seems like it should be a simple fix but I have to admit I am having a hard time understanding Reduce

Thanks in advance for your assistance

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

0

You could add the missing indices to order and map the array by mapping nested array with the indices of order.

let
    data = [['A', 'B', 'C', 'D', 'E', 'F'], ['a1', 'b1', 'c1', 'd1', 'e1', 'f1'], ['a2', 'b2', 'c2', 'd2', 'e2', 'f2'], ['a3', 'b3', 'c3', 'd3', 'e3', 'f3']],
    order = [4, 0, 3],
    seen = new Set(order),
    i = 0;

while (order.length < data[0].length) {
    while (seen.has(i)) ++i;
    order.push(i++);
}

data = data.map(a => order.map(i => a[i]));

data.forEach(a => console.log(...a));

about 4 years ago · Juan Pablo Isaza Denunciar

0

regular mapping seems like it would work:

function myFunction() {
  var originalArray = SpreadsheetApp.getActive().getRange('Sheet1!A:F').getValues();
  var subarray = originalArray.map(e=>[e[4],e[0],e[3]]);
  originalArray = originalArray.map(e=>[e[1],e[2],e[5]]);

}
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