JS 2025 Flashcards
What’s the best way to clone an Object?
structuredClone(objectName)
alternative: JSON.parse(JSON.stringify(objectName) and assign to a new object (=)
How do you protect an Object from any modifications? No changes to values, no new/deleted properties?
Object.freeze(objectName)
How do you protect an Object from new/deleted properties
Object.seal(objectName)
How do you protect an Object from new properties only?
Object.preventExtensions(objectName)
How do you compare the Objects properly?
- JSON.stringify(object1) === JSON.stringify(object2)
- _.isEqual(obj1, obj2)
Objects need to be converted to strings to be compared
Iterating through an object dynamically
People often loop using for…in, but a better way is to get all Keys
console.log(Object.keys(product));
// ✅ [“name”, “specs”]
Iterating through an object dynamically
People often loop using for…in, but a better way is to get all Values
console.log(Object.values(product));
// ✅ [“Laptop”, { ram: “16GB”, storage: “512GB” }]
Iterating through an object dynamically
People often loop using for…in, but a better way is to get all Keys & Values
console.log(Object.entries(product));
// ✅ [[“name”, “Laptop”], [“specs”, { ram: “16GB”, storage: “512GB” }]]