Skip to content

Commit 8887491

Browse files
committed
blog: start of Array.reduce blog
1 parent 24279e5 commit 8887491

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
---
2+
title: 'How to use reduce in JavaScript'
3+
description: 'Learn how to use the reduce function in JavaScript with this step-by-step guide.'
4+
image: 'https://lbycuccwrcmdaxjqyxut.supabase.co/storage/v1/object/public/blog-images/Screenshot%202025-01-07%20at%2023.38.46.png?t=2025-01-07T23%3A38%3A52.593Z'
5+
date: '2025-01-07'
6+
author: 'Logan Ford'
7+
authorImage: 'https://lbycuccwrcmdaxjqyxut.supabase.co/storage/v1/object/public/user-profile-pictures/3a57d7e8-8b80-483b-93d0-70fe1f06b0c0/logo.png?u=1l5im5h5n6e5'
8+
readingTime: 5
9+
status: unpublished
10+
---
11+
12+
import CodeSnippet from '@/components/app/questions/single/layout/code-snippet';
13+
14+
# What is Array reduce in JavaScript?
15+
16+
Array.reduce is a powerful array method in JavaScript. It allows you to 'reduce' (or you can think of it as 'summarize') an array of values into a single value. This
17+
is useful for when you need to get a single value of a list of values.
18+
19+
<br />
20+
21+
<div className="my-4">
22+
Let's take a look at an example:
23+
</div>
24+
25+
<CodeSnippet language="javascript" content={`const numbers = [1, 2, 3, 4, 5];
26+
const sum = numbers.reduce((acc, curr) => acc + curr, 0);
27+
console.log(sum); // 15`} />
28+
29+
<br />
30+
31+
<div className="my-4">
32+
In this example, we are adding up all the numbers in the array to get their total sum. Let's break down how it works:
33+
34+
<br />
35+
36+
The reduce function needs two things:
37+
1. A function that tells it what to do with each number (we tell it to add them together)
38+
2. A starting value (we use 0 as our starting point)
39+
40+
<br />
41+
42+
Think of it like a running total:
43+
- We start with 0 (our initial value)
44+
- First, we add 1 to get 1
45+
- Then we add 2 to get 3
46+
- Then we add 3 to get 6
47+
- Then we add 4 to get 10
48+
- Finally, we add 5 to get 15
49+
50+
<br />
51+
52+
The accumulator (acc) is like our notepad where we keep track of this running total. The current value (curr) is just the next number we're adding to our total.
53+
</div>
54+
55+
<div className="my-4">
56+
### Syntax:
57+
</div>
58+
59+
<CodeSnippet language="javascript" content={`Array.reduce(function(total, currentValue, currentIndex, arr), initialValue)`} />
60+

src/mdx-components.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { MDXComponents } from 'mdx/types';
22
import MdxLink from '@/components/mdx/mdx-link';
33
import MdxHeading from '@/components/mdx/mdx-heading';
4-
4+
import CodeSnippet from '@/components/app/questions/single/layout/code-snippet';
55
import CallToActionBlock from '@/components/marketing/global/call-to-action-block';
66
import MdxList from './components/mdx/mdx-list';
77
import { Button } from './components/ui/button';
@@ -43,6 +43,7 @@ export function useMDXComponents(components: MDXComponents): MDXComponents {
4343
{props.children}
4444
</MdxHeading>
4545
),
46+
CodeSnippet,
4647
CallToActionBlock,
4748
...components,
4849
};

0 commit comments

Comments
 (0)