Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

222
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!