Advertisement
xerocool-101

003 filter method

Apr 20th, 2025 (edited)
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 0.49 KB | Software | 0 0
  1. // Array
  2. const numbers = [1, 2, 3, 4, 5];
  3. const even = numbers.filter((num, index, array) => num % 2 === 0);
  4. console.log(even); // [2, 4]
  5.  
  6. // Object
  7. const users = [
  8.   { id: 1, name: "Alice", active: true },
  9.   { id: 2, name: "Bob", active: false },
  10.   { id: 3, name: "Charlie", active: true }
  11. ];
  12.  
  13. const activeUsers = users.filter(user => user.active);
  14.  
  15. console.log(activeUsers);
  16. // [
  17. //   { id: 1, name: "Alice", active: true },
  18. //   { id: 3, name: "Charlie", active: true }
  19. // ]
  20.  
Tags: JavaScript
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement