📋 Task Management = Array Methods
Task management needs data manipulation. JavaScript array methods make it easy to manage tasks.
📝 Task Management
class TaskManager {
constructor() {
this.tasks = [];
this.id = 0;
}
// CRUD Operations
addTask(title, priority = 'medium') {
const task = {
id: ++this.id,
title,
priority,
completed: false,
createdAt: new Date()
};
this.tasks.push(task);
return task;
}
getTasks() {
return this.tasks;
}
getTask(id) {
return this.tasks.find(task => task.id === id);
}
updateTask(id, updates) {
const task = this.getTask(id);
if (task) {
Object.assign(task, updates);
return task;
}
return null;
}
deleteTask(id) {
const index = this.tasks.findIndex(task => task.id === id);
if (index !== -1) {
return this.tasks.splice(index, 1)[0];
}
return null;
}
// Query Operations
getCompletedTasks() {
return this.tasks.filter(task => task.completed);
}
getPendingTasks() {
return this.tasks.filter(task => !task.completed);
}
getTasksByPriority(priority) {
return this.tasks.filter(task => task.priority === priority);
}
sortTasksByPriority() {
const order = { high: 0, medium: 1, low: 2 };
return [...this.tasks].sort((a, b) => order[a.priority] - order[b.priority]);
}
getTaskSummary() {
const summary = this.tasks.reduce((acc, task) => {
if (task.completed) {
acc.completed++;
} else {
acc.pending++;
}
return acc;
}, { completed: 0, pending: 0 });
return summary;
}
searchTasks(query) {
const searchTerm = query.toLowerCase();
return this.tasks.filter(task =>
task.title.toLowerCase().includes(searchTerm)
);
}
groupByPriority() {
return this.tasks.reduce((acc, task) => {
if (!acc[task.priority]) {
acc[task.priority] = [];
}
acc[task.priority].push(task);
return acc;
}, {});
}
getRecentTasks(days = 7) {
const cutoff = new Date();
cutoff.setDate(cutoff.getDate() - days);
return this.tasks.filter(task =>
task.createdAt >= cutoff
);
}
}
🎯 UI Integration
// Render Tasks
function renderTasks(tasks) {
const container = document.getElementById('task-list');
container.innerHTML = tasks.map(task => `
${task.title}
${task.priority}
`).join('');
}
// Task Statistics
function updateStats(tasks) {
const total = tasks.length;
const completed = tasks.filter(t => t.completed).length;
const pending = total - completed;
document.getElementById('total-tasks').textContent = total;
document.getElementById('completed-tasks').textContent = completed;
document.getElementById('pending-tasks').textContent = pending;
document.getElementById('completion-rate').textContent =
total ? Math.round((completed / total) * 100) + '%' : '0%';
}
// Filter Tasks
function filterTasks(filter) {
let filtered = manager.getTasks();
switch(filter) {
case 'completed':
filtered = manager.getCompletedTasks();
break;
case 'pending':
filtered = manager.getPendingTasks();
break;
case 'high':
filtered = manager.getTasksByPriority('high');
break;
}
renderTasks(filtered);
}
💡 Task Management Tips
- Use array methods for data manipulation
- Keep state management simple
- Update UI reactively
- Use localStorage for persistence
- Handle edge cases gracefully
Array methods make data manipulation elegant and readable. They’re essential for building interactive applications.

