Remove Duplicates from Array
Get unique values from an array using Set
const unique = (arr) => [...new Set(arr)];
// Usage
const numbers = [1, 2, 2, 3, 4, 4, 5];
console.log(unique(numbers)); // [1, 2, 3, 4, 5]Useful code snippets for common programming tasks
Get unique values from an array using Set
const unique = (arr) => [...new Set(arr)];
// Usage
const numbers = [1, 2, 2, 3, 4, 4, 5];
console.log(unique(numbers)); // [1, 2, 3, 4, 5]Delay function execution until after wait time
function debounce(func, wait) {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}
// Usage
const handleInput = debounce((e) => {
console.log(e.target.value);
}, 300);Reusable fetch function with error handling
async function fetchAPI<T>(url: string, options?: RequestInit): Promise<T> {
const response = await fetch(url, {
headers: { 'Content-Type': 'application/json' },
...options,
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
}
// Usage
const data = await fetchAPI<User[]>('/api/users');Concise way to create lists in Python
# Square numbers
squares = [x**2 for x in range(10)]
# Filter even numbers
evens = [x for x in range(20) if x % 2 == 0]
# Nested loops
matrix = [[i+j for j in range(3)] for i in range(3)]Multiple ways to center content with CSS
/* Flexbox */
.flex-center {
display: flex;
align-items: center;
justify-content: center;
}
/* Grid */
.grid-center {
display: grid;
place-items: center;
}
/* Absolute */
.absolute-center {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}