Frontend School
DashboardProblemsLearnPracticeMock InterviewReportsStatsBuy Credits
DashboardProblemsLearnPracticeMock Interview
JavaScript for Frontend Interviews
  1. 1.Introduction to JavaScript & Setup
  2. 2.Variables and Data Types
  3. 3.Operators and Expressions
  4. 4.Control Flow: Conditionals
  5. 5.Control Flow: Loops
  6. 6.Functions: Basics
  7. 7.Arrays and Objects
  8. 8.Higher-Order Functions & Callbacks
  9. 9.Scope and Closures
  10. 10.`this` Keyword and Context
  11. 11.Asynchronous JavaScript: Callbacks & Promises
  12. 12.ES6+ Features (Part 1)
  13. 13.ES6+ Features (Part 2)
  14. 14.Error Handling & Debugging
  15. 15.Asynchronous JavaScript: Async/Await
  16. 16.Prototypal Inheritance & `new` Keyword
  17. 17.Event Loop, Microtasks & Macrotasks
  18. 18.Working with the DOM
  19. 19.Advanced Array Methods & Data Structures
  20. 20.Frontend Interview Prep & Best Practices
JavaScript for Frontend Interviews
07

JavaScript · Lesson 7 of 20 · 4 min

Arrays and Objects

Your ability to manipulate arrays of objects is the single most tested JavaScript skill in frontend interviews.

After this, you'll know exactly which data structure to use and how to transform data from one shape to another—a task at the heart of most coding challenges.

Choose the right structure

Your first decision is always: array or object? The answer depends on how you need to access the data.

Use an array for an ordered list of items. If the order matters and you'll access items by their position (the first, the third, the last), you want an array. Think of a list of comments, steps in a multi-step form, or a history of user actions.

Use an object for a collection of named properties. If you need to look up values by a specific key, you want an object. Think of a user profile (user.name), application configuration (config.apiKey), or any entity where data has descriptive labels.

The pro move: When you need a dictionary or a map for arbitrary keys, consider using a Map object. Unlike a plain object, Map guarantees key insertion order, allows keys of any type (not just strings), and provides a size property. Using a Map shows you're aware of the modern, purpose-built tool for the job.

Data manipulation is the main event

In an interview, you'll rarely be asked to just create an array. You'll be given a realistic data structure, like a response from an API, and asked to transform it. This is the core of the problem.

Most of the time, this means you'll get an array of objects. Your task is to filter it, reshape it, or aggregate data from it.

Here’s a typical scenario: take a list of products and group them by category. This is a classic "transform an array into an object" problem. It tests your ability to iterate over a collection and build a new data structure. Using reduce is the standard, concise way to solve this.

const products = [
  { id: 1, name: "Laptop", price: 1200, category: "Electronics", inStock: true },
  { id: 3, name: "Keyboard", price: 75, category: "Electronics", inStock: false },
  { id: 4, name: "Desk Chair", price: 300, category: "Furniture", inStock: true },
  { id: 6, name: "Bookshelf", price: 150, category: "Furniture", inStock: false },
];

const groupedByCategory = products.reduce((acc, product) => {
  const { category } = product;
  // If the category key doesn't exist in the accumulator, create it.
  if (!acc[category]) {
    acc[category] = [];
  }
  // Push the current product into the correct category array.
  acc[category].push(product);
  return acc;
}, {});

/*
groupedByCategory is now:
{
  Electronics: [
    { id: 1, name: "Laptop", ... },
    { id: 3, name: "Keyboard", ... }
  ],
  Furniture: [
    { id: 4, name: "Desk Chair", ... },
    { id: 6, name: "Bookshelf", ... }
  ]
}
*/

Mastering map, filter, and reduce is non-negotiable. We'll cover them in detail in the next lesson, but recognize them as your primary tools for data manipulation.

Avoid mutation with copies

The trap: Arrays and objects are reference types. Assigning one to a new variable doesn't create a copy; it creates another pointer to the same object in memory.

const original = ['a']; const copy = original; copy.push('b');

Now original is ['a', 'b']. This is a huge source of bugs, especially in frameworks like React where state immutability is a core principle. You must never mutate state directly.

To avoid this, create a shallow copy. The spread syntax (...) is the modern way to do this for both arrays and objects.

// For arrays
const originalArray = [1, 2, 3];
const shallowCopyArray = [...originalArray];

// For objects
const originalObject = { name: "Alice", age: 30 };
const shallowCopyObject = { ...originalObject };

A common follow-up question is, "What if the object or array is nested?" Spread syntax only creates a shallow copy. If an array contains objects, the new array will contain references to the same original objects. Creating a "deep copy" is more complex. The quick-and-dirty method is JSON.parse(JSON.stringify(obj)), but be prepared to discuss its limitations: it throws away functions, undefined values, and Date objects become strings.

Know the performance trade-offs

For small arrays, any method is fine. For large ones, your choice of method matters.

Adding or removing from the end of an array with push and pop is fast. It's an O(1) operation.

Adding or removing from the beginning with unshift and shift is slow. It's an O(n) operation because every other element in the array must be re-indexed. If you're processing a large list, avoid unshift in a loop.

The most important optimization is for lookups. Searching for an item in an array with find() or indexOf() is an O(n) operation. In a loop, that quickly becomes O(n²).

If you need to perform many lookups, convert the array to an object or Map first. Building the lookup object is O(n), but each subsequent lookup is O(1).

Imagine you have 10,000 products and need to find 100 of them by ID. Using find() inside a loop is roughly 100 * 10,000 operations. Instead, create an object where keys are product IDs. This takes one pass (10,000 operations) to build, then 100 fast lookups. The difference is enormous. This is a classic interview trade-off question.

Try it

Take the products array from the example. Write a function that returns a single object summarizing the inventory. The object should have two keys: inStockCount (the number of products with inStock: true) and totalValue (the sum of price for all in-stock products). Try to do this in a single pass using reduce.

Next: We'll take a closer look at the tools you just used, filter and reduce, in Higher-Order Functions & Callbacks.

Lock it in — apply this lesson under interview conditions.

Try an AI mock interview
Previous

Functions: Basics

Next

Higher-Order Functions & Callbacks