First Principles
Programming

Array Manipulation in Javascript

The array methods you actually reach for — map, filter, reduce, find, push/pop, slice/splice, forEach, some, and every.

By Fadjar Rafi. Published 05 Jan 2025. 3 min read.

Contents
  1. map
  2. filter
  3. reduce
  4. find and findIndex
  5. push, pop, shift, and unshift
  6. slice and splice
  7. forEach
  8. some and every
  9. Wrapping up

Arrays show up everywhere, and JavaScript gives you a set of methods for working with them without writing loops by hand. The ones you'll reach for most are map, filter, reduce, find, findIndex, push, pop, shift, unshift, slice, splice, forEach, some, and every. Let's go through them.

The three you'll use constantly are map, filter, and reduce — transform each item, keep the ones you want, or fold everything into one value:

map transforms each element, filter keeps matching elements, reduce combines them into one value

map#

map() runs each element through a function and returns a new array of the results — same length, transformed values. Say we want to double every number:

const numbers = [1, 2, 3, 4]
const doubled = numbers.map((num) => num * 2)
// [2, 4, 6, 8]

It's also the go-to for pulling one field out of a list of objects:

const users = [
  { id: 1, name: 'John' },
  { id: 2, name: 'Jane' },
  { id: 3, name: 'Doe' },
]
const userIds = users.map((user) => user.id)
// [1, 2, 3]

filter#

filter() returns a new array with only the elements that pass a test. The callback returns true to keep an element, false to drop it:

const ages = [15, 25, 35, 12, 40]
const adults = ages.filter((age) => age >= 18)
// [25, 35, 40]

Here we keep only the ages that are 18 or above.

reduce#

reduce() folds an array down to a single value. It walks each element while carrying an accumulator, and you decide how the two combine:

const prices = [10, 20, 30]
const total = prices.reduce((sum, price) => sum + price, 0)
// 60

The accumulator starts at 0, then each price is added onto it until we're left with 60.

find and findIndex#

Both look for the first element that matches a condition:

  • find() returns the element itself.
  • findIndex() returns its position in the array.
const users = [
  { id: 1, name: 'John' },
  { id: 2, name: 'Jane' },
]

const john = users.find((user) => user.name === 'John')
const johnIndex = users.findIndex((user) => user.name === 'John')

push, pop, shift, and unshift#

These change the original array by adding or removing an element at one end — the building blocks for stacks and queues:

const fruits = ['apple']

fruits.push('banana') // add to the end: ['apple', 'banana']
fruits.pop() // remove from the end: ['apple']
fruits.unshift('orange') // add to the front: ['orange', 'apple']
fruits.shift() // remove from the front: ['apple']

slice and splice#

Similar names, different jobs — and one of them mutates:

// slice: copies a portion, leaves the original alone
const numbers = [1, 2, 3, 4, 5]
const subset = numbers.slice(1, 3) // [2, 3]

// splice: changes the array in place, removing/replacing elements
const letters = ['a', 'b', 'c']
letters.splice(1, 1, 'x') // replaces 'b' with 'x' → ['a', 'x', 'c']

forEach#

forEach() runs a function for every element without building a new array. Use it for side effects — logging, updating something outside the loop:

const items = ['book', 'pen', 'desk']

items.forEach((item, index) => {
  console.log(`Item ${index + 1}: ${item}`)
})

some and every#

Both return a boolean after testing the elements:

  • some() — is at least one element a match?
  • every() — do all of them match?
const scores = [75, 80, 95]

const hasPassingScore = scores.some((score) => score >= 90) // true
const allPassed = scores.every((score) => score >= 70) // true

Wrapping up#

Most day-to-day array work comes down to picking the right method: map to transform, filter to narrow down, reduce to combine, and the rest for the smaller jobs in between. Reach for these instead of manual loops and your code stays shorter and easier to follow.