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

105
Views
Leave objects by the list of keys. Pure JS

I want to create a new array of objects without the gender field. Where is the mistake?

let collections = [
    { name: "Tom", gender: "male", age: 12 },
    { name: "Becky", gender: "female", age: 11 },
    { name: "Huck", gender: "male", age: 13 }
  ];
  let newCollections = collections.slice();
  let operations = {
    select: function () {
      let args = [].slice.call(arguments);
      for (let i = 0; i < newCollections.length; i++) {
        for (let key in newCollections[i]) {
          for (let j = 0; j < args.length; j++) {
            if (key !== args[j]) {
              delete key;
              return newCollections;
            } } } } } }; 
const result = operations.select("name", "age");// the list of fields to save

console.log(result);

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can do it using map method:

const array_without_gender = collections.map(el => {
    return {name: el.name, age: el.age};
})

This code will create new array filled by objects without gender key,

about 4 years ago · Juan Pablo Isaza Report

0

You could create an omit() function that will return an object without a given property.

We can then use Array.map() to run each item in the array through this:

let collections = [ { name: "Tom", gender: "male", age: 12 }, { name: "Becky", gender: "female", age: 11 }, { name: "Huck", gender: "male", age: 13 } ];

function omit(obj, field) {
    return (({ [field]: _, ...result }) => result)(obj);
}

let newCollections = collections.map(person => omit(person, 'gender'));
console.log('New collections:', newCollections);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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!