
ReactJS
The `map()` function is the primary way to transform arrays into lists of React elements.
function TodoList() {
const todos = ["Learn React", "Build Projects", "Master Hooks"];
return (
<ul>
{todos.map((todo, index) => (
<li key={index}>{todo}</li>
))}
</ul>
);
}function UserList() {
const users = [
{ id: 1, name: "Alice" },
{ id: 2, name: "Bob" },
{ id: 3, name: "Charlie" }
];
return (
<ul>
{users.map(user => (
<li key={user.id}>{user.name}</li>
))}
</ul>
);
}function ProductGrid() {
const products = [
{ id: 1, name: "Laptop", price: 999 },
{ id: 2, name: "Phone", price: 599 }
];
return (
<div className="grid">
{products.map(product => (
<ProductCard key={product.id} product={product} />
))}
</div>
);
}
function ProductCard({ product }) {
return (
<div>
<h3>{product.name}</h3>
<p>${product.price}</p>
</div>
);
}| Pattern | Use Case |
|---|---|
| `items.map(item => ...)` | Simple transformation |
| `items.map((item, i) => ...)` | Need index |
| `items.map(item => <Component key={item.id} {...item} />)` | Component rendering |
function NestedLists() {
const categories = [
{ id: 1, name: "Fruits", items: ["Apple", "Banana"] },
{ id: 2, name: "Veggies", items: ["Carrot", "Broccoli"] }
];
return (
<div>
{categories.map(category => (
<div key={category.id}>
<h2>{category.name}</h2>
<ul>
{category.items.map((item, i) => (
<li key={i}>{item}</li>
))}
</ul>
</div>
))}
</div>
);
}Resources
Ojasa Mirai
Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.
Learn Deep • Build Real • Verify Skills • Launch Forward