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

139
Views
How to create a function that omits key/value pairs from an object, and creates a new object with the remaining properties and values?

I've been working on creating an omit function with a source and keys parameter, that checks for keys within the source, and if they are found, those properties are omitted from the source, then create a new object literal with the remaining key/value pairs within the source. So far, I've been unsuccessful, and only have been able to create a function that does the opposite of creating an object with the keys found within the source. Can anyone help figure out what I'm missing? It would be very appreciated!

function omit(source, keys) {
  var includedSource = {};
  for (var key in source) {
    for (var i = 0; i < keys.length; i++) {
      if (keys[i] !== key) {
        includedSource[key] = source[key];
        console.log('source[keys[i]]:', source[keys[i]]);
        console.log('includedSource:', includedSource);
      }
    }
  }
  return includedSource;
}

  function omit(source, keys) {
    var includedSource = {};
    for (var key in source) {
      for (var i = 0; i < keys.length; i++) {
        if (keys[i] === key) {
          includedSource[key] = source[key];
          console.log('source[keys[i]]:', source[keys[i]]);
          console.log('includedSource:', includedSource);
          }
        }
     }
     return includedSource;
   }
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Here is a simple implementation with flat object. Im not sure how would keys be declared if your object might be nested, and you would expect to extract some of N level of nested value/keys

const obj = {
  A: 1,
  B: 2,
  C: 3,
  D: 4
};

function omit(data, keys) {
  let result = {};

  for (let key in data) {
    if (keys.includes(key)) continue;

    result[key] = data[key];
  }

  return result;
}

console.log(omit(obj, ["A", "D"])) // {B: 2, C: 3}
about 4 years ago · Juan Pablo Isaza Report

0

You could destructure and rest for a new object.

function omit(object, keys) {
    let _; // prevent to be global
    for (const key of keys) ({ [key]: _, ...object } = object);
    return object;
}

console.log(omit({ a: 1, b: 2, c: 3, d: 4 }, ['b', 'c']));

about 4 years ago · Juan Pablo Isaza Report

0

One approach is use Object.entries() and filter the entries out that have the unwanted keys then use Object.fromEntries() to create the new object from the filtered entries

const omit = (data, keys) => {
  return Object.fromEntries(
    Object.entries(data).filter(([k]) => !keys.includes(k))
  )
}


console.log(omit({ a:1,b:2,c:3,d:4}, ['a', 'b']))

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!