Declarative Data Transformation
Transform data with map, filter, and reduce.
Declarative Data Transformation is a free JavaScript Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Declarative vs Imperative
Imperative code says HOW (loops, counters, mutation). Declarative code says WHAT. Array methods like map, filter, and reduce let you describe transformations without manual loops.
const nums = [1, 2, 3, 4];
const doubled = nums.map((n) => n * 2);
console.log(doubled); // [2, 4, 6, 8]map: Transform Each Element
map returns a new array of the same length with each element transformed by your function. The original is untouched.
const names = ["ada", "bob"];
const upper = names.map((n) => n.toUpperCase());
console.log(upper); // ["ADA", "BOB"]
console.log(names); // unchangedfilter: Keep Some Elements
filter returns a new array containing only elements for which your predicate returns truthy.
const nums = [1, 2, 3, 4, 5, 6];
const evens = nums.filter((n) => n % 2 === 0);
console.log(evens); // [2, 4, 6]reduce: Fold Into One Value
reduce collapses an array into a single value by accumulating. Provide a reducer and an initial value.
const nums = [1, 2, 3, 4];
const sum = nums.reduce((acc, n) => acc + n, 0);
console.log(sum); // 10Chaining Operations
Because each method returns a new array, you can chain them into a readable pipeline that reads top to bottom.
const nums = [1, 2, 3, 4, 5, 6];
const result = nums
.filter((n) => n % 2 === 0)
.map((n) => n * 10);
console.log(result); // [20, 40, 60]A Realistic Pipeline
Combine filter, map, and reduce to answer a question about a dataset in one expression.
const orders = [
{ item: "pen", price: 3, paid: true },
{ item: "ink", price: 8, paid: false },
{ item: "pad", price: 5, paid: true }
];
const paidTotal = orders
.filter((o) => o.paid)
.map((o) => o.price)
.reduce((a, p) => a + p, 0);
console.log(paidTotal); // 8Mapping to New Shapes
map can reshape objects, not just numbers. Project each record into the fields you care about.
const users = [{ id: 1, name: "Ada", age: 36 }];
const summary = users.map((u) => ({ id: u.id, label: u.name }));
console.log(summary); // [{ id: 1, label: "Ada" }]reduce Can Build Anything
reduce is not just for sums. It can build objects, group data, or even reimplement map and filter.
const words = ["a", "bb", "a", "ccc"];
const counts = words.reduce((acc, w) => {
acc[w] = (acc[w] || 0) + 1;
return acc;
}, {});
console.log(counts); // { a: 2, bb: 1, ccc: 1 }Order Matters
Filtering before mapping avoids transforming elements you will throw away. Put cheap filters early in the chain for clarity and efficiency.
const data = [1, 2, 3, 4, 5];
const out = data
.filter((n) => n > 2) // shrink first
.map((n) => n * n); // then transform
console.log(out); // [9, 16, 25]No Mutation Anywhere
These methods never change the source array. That makes pipelines safe and predictable, fitting the functional style perfectly.
const src = [3, 1, 2];
const sorted = [...src].sort((a, b) => a - b);
console.log(sorted); // [1, 2, 3]
console.log(src); // [3, 1, 2] untouchedflatMap for Nested Results
When a map produces arrays, flatMap maps and flattens one level in a single step.
const pairs = [1, 2, 3].flatMap((n) => [n, n * 10]);
console.log(pairs); // [1, 10, 2, 20, 3, 30]Quick Check
Declarative transformations.
Recap
Declarative transformation describes WHAT, not HOW. map transforms, filter selects, and reduce folds to one value; chain them into readable pipelines. None mutate the source, filtering early is efficient, and reduce can build any structure. Reach for flatMap when a map yields arrays.
Frequently asked questions
Is the “Declarative Data Transformation” lesson free?
Yes — the full text of “Declarative Data Transformation” is free to read here on the web, and the JavaScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the JavaScript Academy course, upgrade to CoddyKit PRO.
What will I learn in “Declarative Data Transformation”?
Transform data with map, filter, and reduce. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start JavaScript Academy?
No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Declarative Data Transformation” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this JavaScript Academy lesson?
Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Pure Functions and Side Effects
- Declarative Data Transformation
- Avoiding Mutation
- Point-Free Style