-3

I have a JS object that i want to log to the console but one of its values is 5 million characters of a hashed image so im not really interessted in that. Is there a way to exclude this key from the console log / is there a short form for cloning the object, deleting the value and logging it?

Not a duplicate of "cloning object except for one key" btw

el_Yanuki
  • 5
  • 2
  • Given that the answer is “no, other than cloning the object except for that key”, is it in fact a duplicate of that? (You could serialize it to a string directly one way or another I guess, but you’ll lose all of the object inspection console features.) – Ry- Sep 07 '22 at 22:37
  • One would utilize the [rest property within a destructuring assignment](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#rest_property) ... for instance like ... `function logObjectWithoutKey(key, obj) { const { [ key ]: suppressed, ...rest } = obj; console.log(rest); }` ... and then e.g ... `logObjectWithoutKey('img', { foo: 'foo', bar: 'bar', baz: 'baz', img: 'a lot of data' });` ... or ... `logObjectWithoutKey('baz', { foo: 'foo', bar: 'bar', baz: 'baz', img: 'a lot of data' });` – Peter Seliger Sep 07 '22 at 23:23

1 Answers1

-2

One very easy way to do this is by using the spread operator [MDN Docs]

const yourObject = {id:"something",img: "5M-character-stuff"}
console.log({...yourObject, img: "short-img"});
// logs: {id:"something",img: "short-img"}
FortyGazelle700
  • 124
  • 3
  • 9