I need to set all properties of some object to null
. But the object can be very big, so I can't just do it one by one.
How to set all properties at once?
Javascript null is a primitive type that has one value null. JavaScript uses the null value to represent a missing object. Use the strict equality operator ( === ) to check if a value is null . The typeof null returns 'object' , which is historical bug in JavaScript that may never be fixed.
An object of a class cannot be set to NULL; however, you can set a pointer (which contains a memory address of an object) to NULL.
Strange result: null vs 0 Comparisons convert null to a number, treating it as 0 . That's why (3) null >= 0 is true and (1) null > 0 is false. On the other hand, the equality check == for undefined and null is defined such that, without any conversions, they equal each other and don't equal anything else.
In Java, a null value can be assigned to an object reference of any type to indicate that it points to nothing. The compiler assigns null to any uninitialized static and instance members of reference type. In the absence of a constructor, the getArticles() and getName() methods will return a null reference.
Here's a useful function called 'Object.keys()', it returns all of the attribute names of an object.
let setAll = (obj, val) => Object.keys(obj).forEach(k => obj[k] = val); let setNull = obj => setAll(obj, null);
Non-arrow-function version:
function setAll(obj, val) { /* Duplicated with @Maksim Kalmykov for(index in obj) if(obj.hasOwnProperty(index)) obj[index] = val; */ Object.keys(obj).forEach(function(index) { obj[index] = val }); } function setNull(obj) { setAll(obj, null); }
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