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

226
Vistas
How to build a new Array of Objects from existing Array of Objects, filtered to match a list of keys

I have an array of objects, example:

[
  {
    "fName": "Jon",
    "lName": "Doe",
    "age": "30",
    "shirtSize": "M"
  },
  {
    "fName": "Jane",
    "lName": "Foe",
    "age": "25",
    "shirtSize": "M"
  },
  ...
]

I also have a list of keys, example:

["age", "shirtSize"]

I want to take the array of objects, and build a new array of objects that match the key list array, effectively filtering the array of objects to only have the key/value pairs I want. The above are examples, and the real datasets are of course more verbose. I've brute forced it in my algorithm below and as expected, it's not very performant at all, as a result of my loop within the map. My result is like:

    [
      {
        "age": "30",
        "shirtSize": "M"
      },
      {
        "age": "25",
        "shirtSize": "M"
      },
      ...
    ]

I am not using anything like lodash. How can I improve this performance?

const data = [
  {
    "fName": "Jon",
    "lName": "Doe",
    "age": "30",
    "shirtSize": "M"
  },
  {
    "fName": "Jane",
    "lName": "Foe",
    "age": "25",
    "shirtSize": "M"
  }
];
const keyList = ["age", "shirtSize"];

const filteredDataset = data.map((elem) => {
  let tempHolder = {};
  for (let key of keyList) {
    tempHolder[key] = elem[key];
  }
  return tempHolder;
});

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

0

var results = [];
for(var person of data){
    var obj = {};
    for(var key of keyList){
        obj[key] = person[key];
    }
    results.push(obj);
}

The first loop iterates through data, and the inner loop iterates through keyList and adds those attributes to the temporary object, which is then pushed to the results array.

EDIT: Your code is basically the same, with yours just using map to iterate through instead of a for...of loop.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can express the same algorithm more declaratively:

const select = (props) => (objs) =>
  objs .map (o => Object .fromEntries (props .map (p => [p, o[p]])))

const items = [{fName: "Jon", lName: "Doe", age: "30", shirtSize: "M"}, {fName: "Jane", lName: "Foe", age: "25", shirtSize: "M"}]

console .log (select (["age", "shirtSize"]) (items))

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