How can I remove prototype fields from an object besides via this method ?
const input = {foo: 'bar', __proto__: {unwanted: 'things'}}
expect(JSON.parse(JSON.stringify(input))).toEqual({foo: 'bar'}) // true
// this works but is there a cleaner way ?
it depends what you trying to achieve, but I would generally recommend .hasOwnProperty for checking whether the field is a prototype field
reference: MDN
you can use Object.create and pass to it null which will create clean object without prototype property, then you can create your properties for that object, but note that you can't use Object.prototype methods like hasOwnProperty(), toString(), valueOf() and so on
const input = Object.create(null);
input.foo = 'bar';
console.log(input);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With