-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

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.) Commented Sep 7, 2022 at 22:37
  • One would utilize the rest property within a destructuring assignment ... 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' }); Commented Sep 7, 2022 at 23:23

1 Answer 1

-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"}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.