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

178
Views
console.log() an object stripped of selected keys

I have this:

let obj= {
   'Cow' : 'Moo',
   'Cat' : 'Meow',
   'Dog' : 'Bark'
};

I want to console.log(JSON.stringify(obj_without_Cow)), so this would be logged:

{
   'Cat' : 'Meow',
   'Dog' : 'Bark'
}

I could delete Cow, destructure with rest, or use several other approaches, but I was wondering if there is a way to only modify what is passed to console.log, as shown above. That is, without extra code outside of console.log().

Analogically, if I was logging a str = 'xxxyyy', I could have all 'x's removed: console.log(str.replaceAll('x','')), it is intuitive to try console.log(delete obj.Cow) which, however, return true or false, not the modified object.

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

0

without extra code outside of console.log().

An IIFE does wonders for one-liners:

console.log( (({Cow, ...withoutCow}) => JSON.stringify(withoutCow))(obj) );

Alternatively, you can use the replacer parameter to JSON.stringify:

console.log(JSON.stringify(obj, (key, val) => key == 'Cow' ? undefined : val));
about 4 years ago · Juan Pablo Isaza Report

0

It seems that you need a console.log interceptor:

console.log = (() => {
    const logger = console.log;
    return function (...args) {
        if (typeof args[0] === 'object')
            args[0] = Object.fromEntries(Object.entries(args[0]).filter(([key]) => !key.includes('Cow')));
        else if (typeof args[0] === 'string')
            args[0] = args[0].replaceAll('x','');
        return logger.apply(this, args);
    };
})();


let obj = {
   'Cow' : 'Moo',
   'Cat' : 'Meow',
   'Dog' : 'Bark'
};

let str = 'xxxyyy';

console.log(obj);
console.log(str);

about 4 years ago · Juan Pablo Isaza Report

0

If you are using Underscore.js or Lodash, there is a function 'omit' that will do it. https://lodash.com/docs#omit

const thisIsObject= {
    'Cow' : 'Moo',
    'Cat' : 'Meow',
    'Dog' : 'Bark'
};

console.log(_.omit(thisIsObject,'Cow'));

=> {'Cat' : 'Meow', 'Dog' : 'Bark'}

Or you can create your own omit function from pure JavaScript.

const omit = (inputObject, removedKeys) =>
  Object.keys(inputObject)
    .filter((key) => !removedKeys.includes(key))
    .reduce((obj, key) => {
      obj[key] = inputObject[key];
      return obj;
    }, {});


console.log(omit(thisIsObject,'Cow'));

=> {'Cat' : 'Meow', 'Dog' : 'Bark'}
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!