---
title: "How to Return Multiple Items in a Map Function in JavaScript - CloudBlast"
description: "How to Return Multiple Items in a Map Function in JavaScript"
url: "https://cloudblast.io/fr/article/How-to-Return-Multiple-Items-in-a-Map-Function-in-JavaScript"
language: "fr-FR"
---

BLOG

# How to Return Multiple Items in a Map Function in JavaScript

5€ DE SOLDE OFFERT à l'inscription

Inscrivez-vous sur CloudBlast et recevez 5€ de crédit cloud pour déployer vos VPS

[Recevoir 5€ gratuits](https://console.cloudblast.io/auth/login?ref=2)

-   [Retour](https://cloudblast.io/fr)
-   [Tous les articles](https://cloudblast.io/fr/blog)

2024-09-29

### How to Return Multiple Items in a Map Function in JavaScript

tutorials

**Introduction**
The \`map()\` function in JavaScript is a powerful tool for transforming arrays. It allows you to iterate over each element in an array, apply a transformation, and return a new array with the modified elements. However, there might be scenarios where you want to return multiple items for each element processed by the \`map()\` function. This guide explores how to achieve this, providing practical examples and insights into using \`map()\` effectively.

**Understanding the Map Function**
The \`map()\` function creates a new array populated with the results of calling a provided function on every element in the calling array. It does not modify the original array but returns a new one with the transformed elements.

**Returning Multiple Items**
By default, the \`map()\` function returns a single item for each element in the array. However, if you need to return multiple items, you can achieve this by returning an array or an object from the callback function. Here’s how you can do it:

**1\. Returning an Array of Items**
You can return an array of items for each element processed by \`map()\`. This approach is useful when you want to expand each element into multiple elements in the resulting array.

> const numbers = \[1, 2, 3\];
> const expanded = numbers.map(num => \[num, num \* 2\]);
> console.log(expanded); // Output: \[\[1, 2\], \[2, 4\], \[3, 6\]\]

In this example, each number is transformed into an array containing the number and its double. The resulting array is a nested array, where each sub-array contains the multiple items returned for each original element.

**2\. Flattening the Result**
If you want a flat array instead of a nested one, you can use the \`flatMap()\` method. This method combines \`map()\` and \`flat()\` into a single operation, flattening the result by one level.

> const numbers = \[1, 2, 3\];
> const expandedFlat = numbers.flatMap(num => \[num, num \* 2\]);
> console.log(expandedFlat); // Output: \[1, 2, 2, 4, 3, 6\]

Here, \`flatMap()\` is used to return a flat array with all the items, effectively flattening the nested arrays into a single-level array.

**3\. Returning an Object**
Alternatively, you can return an object for each element, which allows you to associate multiple values with keys.

> const numbers = \[1, 2, 3\];
> const objects = numbers.map(num => ({ original: num, double: num \* 2 }));
> console.log(objects); // Output: \[{ original: 1, double: 2 }, { original: 2, double: 4 }, { original: 3, double: 6 }\]

In this example, each number is transformed into an object containing the original number and its double, providing a structured way to return multiple values.

**Conclusion**
While the \`map()\` function in JavaScript is designed to return a single item for each element, you can effectively return multiple items by using arrays or objects. By leveraging these techniques, you can transform your data in versatile ways, enhancing the functionality and flexibility of your JavaScript applications. Whether you need nested arrays, flat arrays, or structured objects, understanding how to return multiple items in a \`map()\` function is a valuable skill for any JavaScript developer.

Partager

## Continuer la lecture

[2026-07-25 How to Install and Set Up Mailcow on Ubuntu and Debian VPS tutorials](https://cloudblast.io/article/how-to-install-and-setup-mailcow-on-ubuntu-and-debian-vps)

[2026-07-25 How to Install Chatwoot on the Latest Ubuntu and Debian Server tutorials](https://cloudblast.io/article/how-to-install-chatwoot-on-ubuntu-and-debian-server)

[2026-07-25 How to Install Mattermost on the Latest Ubuntu and Debian Server tutorials](https://cloudblast.io/article/how-to-install-mattermost-on-ubuntu-and-debian-server)

[2026-07-25 How to Install RustDesk on the Latest Ubuntu and Debian Server tutorials](https://cloudblast.io/article/how-to-install-rustdesk-on-ubuntu-and-debian-server)
