Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // The .map() method creates a new array by applying a function to each element of the original array.
- // It's like saying: “take each item, do something to it, and return a new array with the results.”
- // Return new array
- const numbers = [1, 2, 3, 4];
- const doubled = numbers.map((num, index, array) => num * 2);
- console.log(doubled); // [2, 4, 6, 8]
- // Object
- const users = [
- { id: 1, name: "Alice" },
- { id: 2, name: "Bob" }
- ];
- const names = users.map(user => user.name);
- console.log(names); // ["Alice", "Bob"]
- // React
- setUsers(users.map((u) => (u._id === editId ? res.data : u)));
- {todos.map(todo => (
- <li key={todo.id}>{todo.text}</li>
- ))}
- //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement