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

79
Vistas
Javascript select and reorder elements in an array

I have a data array like this:

dataArray = [
0: {"Item1" : Val1, "Item2" : Val2, "Item3" : Val3 ... "ItemN" : ValN}
1: {"Item1" : Val1, "Item2" : Val2, "Item3" : Val3 ... "ItemN" : ValN}
.
.
N: {"Item1" : Val1, "Item2" : Val2, "Item3" : Val3 ... "ItemN" : ValN}
]

I have another array which contains a set of arbitrary, variable elements in a desired order:

selArray = ["Item2", "Item9", "Item7"]

The desired output:

outArray = [
0: {"Item2" : Val2, "Item9" : Val9, "Item7" : Val7}
1: {"Item2" : Val2, "Item9" : Val9, "Item7" : Val7}
.
.
N: {"Item2" : Val2, "Item9" : Val9, "Item7" : Val7}
]

I want to select and reorder the elements in dataArray based on the elements in selArray. The contents of both dataArray and selAarray are going to change multiple times in the application, so the selecting on position in the array, or deleting items in the array won't work.

Would appreciate any ideas about how to do this. TIA

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

0

Something like this ought to do you:

function selectProperties( arr, ...keys ) {
  const selection = arr.map( obj =>
    Object.fromEntries(
      Object
      .entries(obj)
      .filter( tuple => arr.includes(tuple[0]) )
    )
  )
}

But it's probably easier just to use Lodash.js and...

const _ = require('lodash');

const selectProperties = ( arr, ...keys ) => arr.map( obj =>
  _.pick(obj,keys)
);

Code you don't write is code you don't have to maintain.

about 4 years ago · Juan Pablo Isaza Denunciar

0

I hope it helps you.

const dataArray = [{
    "Item5": 5,
    "Item6": 6,
    "Item1": 1,
    "Item2": 2,
    "Item3": 3,
    "Item4": 4,
  }, {
    "Item5": 5,
    "Item6": 6,
    "Item1": 1,
    "Item3": 3,
    "Item2": 2,
    "Item4": 4,
  }

]

const selArray = ["Item1", "Item2", "Item3", "Item4", "Item5", "Item6"]

const organized = dataArray.map((elm) => {
  nelm = {};
  selArray.map((elm2) => {
    if (elm.hasOwnProperty(elm2)) {
      return nelm[elm2] = elm[elm2]
    }
  })
  return nelm
})

console.log(organized)

about 4 years ago · Juan Pablo Isaza Denunciar

0

Thanks for the suggestions. My brute force solution is to

  1. convert the data array to an object
  2. filter and reduce the object
  3. reorder the object elements into an array based on selArr:
var inMap = { ...inData }
var outData = [];
for(i = 0; i < inData.length; i++){
  var tmp2 = Object.keys(inMap[i]).
  filter((key) => selArr.includes(key)).
  reduce((cur, key) => { return Object.assign(cur, { [key]: inMap[i][key] })}, {});

  var tmp3 = [];
  for(j = 0; j < selArr.length;j++){
     tmp3[selArr[j]] = tmp2[selArr[j]];
  }
 outData.push(tmp3);
}

This works for the different cases I need to use. AB

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