Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

229
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda