Advertisement
xerocool-101

007 includes method

Apr 20th, 2025 (edited)
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 2.60 KB | Software | 0 0
  1. array.find((element, index, array) => {
  2.   return condition;
  3. });
  4.  
  5. // Array
  6. const numbers = [5, 10, 15, 20];
  7. const found = numbers.find(num => num > 10);
  8. console.log(found); // 15 ✅ (first one > 10)
  9.  
  10. // Object
  11. const users = [
  12.   { id: 1, name: "Alice" },
  13.   { id: 2, name: "Bob" },
  14.   { id: 3, name: "Charlie" }
  15. ];
  16.  
  17. const user = users.find(u => u.id === 2);
  18. console.log(user); // { id: 2, name: "Bob" }
  19.  
  20. // No Match
  21. const result = [1, 2, 3].find(num => num > 5);
  22. console.log(result); // undefined ❗️
  23.  
  24. // React
  25. const UserProfile = ({ id, users }) => {
  26.   const user = users.find(u => u.id === id);
  27.  
  28.   return user ? <h2>{user.name}</h2> : <p>User not found</p>;
  29. };
  30.  
  31. // Find a product by ID in a shopping cart
  32. const cart = [
  33.   { id: 1, name: "Shirt", quantity: 2 },
  34.   { id: 2, name: "Shoes", quantity: 1 }
  35. ];
  36.  
  37. const productId = 2;
  38. const item = cart.find(product => product.id === productId);
  39.  
  40. console.log(item);
  41. // { id: 2, name: "Shoes", quantity: 1 }
  42.  
  43.  
  44. // Find a user by username or ID
  45. const users = [
  46.   { id: 101, username: "alice" },
  47.   { id: 102, username: "bob" }
  48. ];
  49.  
  50. const currentUser = users.find(u => u.username === "bob");
  51.  
  52. console.log(currentUser);
  53. // { id: 102, username: "bob" }
  54.  
  55. // Find a blog post by slug or ID
  56. const posts = [
  57.   { id: 1, slug: "intro-to-react", title: "React Basics" },
  58.   { id: 2, slug: "advanced-hooks", title: "React Hooks" }
  59. ];
  60.  
  61. const post = posts.find(p => p.slug === "advanced-hooks");
  62.  
  63. console.log(post.title); // "React Hooks"
  64.  
  65. // Find an event happening on a specific date
  66. const events = [
  67.   { name: "Hackathon", date: "2025-04-20" },
  68.   { name: "Launch", date: "2025-05-01" }
  69. ];
  70.  
  71. const today = "2025-04-20";
  72. const eventToday = events.find(e => e.date === today);
  73.  
  74. console.log(eventToday?.name); // "Hackathon"
  75.  
  76. // React Example
  77. import React, { useState } from "react";
  78.  
  79. const App = () => {
  80.   const [selectedId, setSelectedId] = useState(2); // Simulate selected user ID
  81.  
  82.   const users = [
  83.     { id: 1, name: "Alice", age: 25, role: "Admin" },
  84.     { id: 2, name: "Bob", age: 30, role: "Editor" },
  85.     { id: 3, name: "Charlie", age: 22, role: "Viewer" },
  86.   ];
  87.  
  88.   // 🔍 Find the user using `.find()`
  89.   const user = users.find((u) => u.id === selectedId);
  90.  
  91.   return (
  92.     <div style={{ padding: "1rem" }}>
  93.       <h1>User Profile</h1>
  94.       {user ? (
  95.         <div>
  96.           <p><strong>Name:</strong> {user.name}</p>
  97.           <p><strong>Age:</strong> {user.age}</p>
  98.           <p><strong>Role:</strong> {user.role}</p>
  99.         </div>
  100.       ) : (
  101.         <p>User not found</p>
  102.       )}
  103.     </div>
  104.   );
  105. };
  106.  
  107. export default App;
  108.  
Tags: JavaScript
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement