Last active
January 24, 2023 17:38
-
-
Save tech-chieftain/44bc2a661cb4ca2e083499b7d0663ff2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ! Examples | |
const product = { | |
name: "Smartphone", | |
brand: "Apple", | |
model: "iPhone 12 Pro", | |
price: 999, | |
color: "Space Gray", | |
storage: "256GB", | |
inStock: true, | |
branch: { | |
address: "Time square", | |
city: "New York", | |
}, | |
}; | |
const prices = [ | |
19.99, 29.99, 39.99, 49.99, 59.99, 69.99, 79.99, 89.99, 99.99, 149.99, | |
]; | |
const prices2 = [ | |
22.99, 33.99, 39.99, 13.99, 59.99, 26.99, 86.99, 56.99, 34.99, 52.99, | |
]; | |
// ! Spread | |
const allPrices = [...prices,...prices2] | |
console.log(allPrices); | |
const discountProduct = { ...product }; | |
console.log("before changes", { discountProduct, product }); | |
discountProduct.model = "iPhone 14 Pro Max"; | |
discountProduct.branch.city = "Boston"; | |
console.log("after changes", { discountProduct, product }); | |
! Rest | |
const sum = (...numbers) => { | |
console.log({ numbers }); | |
return numbers.reduce((acc, current) => acc + current, 0); | |
}; | |
console.log(sum(10, 15, 33, 22, 66, 34, 75)); | |
// ! Destruction | |
const {model, brand, ...rest} = product; | |
console.log(model, brand,rest); | |
const [first, second, ...rest1] = prices; | |
console.log(first, second,rest1); | |
const deepCopy = (obj) => { | |
let copy = {}; | |
if (obj === null || typeof obj !== "object") { | |
return obj; | |
} | |
if (obj instanceof Object) { | |
copy = {}; | |
for (let key in obj) { | |
if (obj.hasOwnProperty(key)) { | |
copy[key] = deepCopy(obj[key]); | |
} | |
} | |
return copy; | |
} | |
}; | |
const deepCopyObject = deepCopy(product); | |
console.log("Before", { deepCopyObject, product }); | |
deepCopyObject.branch.city = "Boston"; | |
console.log("After", { deepCopyObject, product }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment