Course/Chapter 5/2. Filtering & Sorting Data
    intermediate
    Chapter 5: Data Transformation & Manipulation

    2. Filtering & Sorting Data

    Remove unwanted items and order results.

    15m Lesson 2 of 5

    Filtering removes items that don't meet your criteria, while sorting organizes the remaining items. Both are essential for working with datasets from APIs and databases.

    Code Examples

    Filter and Sort in Code Node
    javascript
    const items = $input.all().map(i => i.json);
    
    // Filter: only active users with email
    const filtered = items.filter(u => 
      u.status === "active" && u.email
    );
    
    // Sort: by created_at descending
    filtered.sort((a, b) => 
      new Date(b.created_at) - new Date(a.created_at)
    );
    
    // Deduplicate by email
    const seen = new Set();
    const unique = filtered.filter(u => {
      if (seen.has(u.email)) return false;
      seen.add(u.email);
      return true;
    });
    
    return unique.map(json => ({ json }));

    Pro Tips

    • 💡For simple filters, use the IF node instead of writing code

    Comprehension Quiz

    Answer 2 of 2 correctly to unlock lesson completion.

    1. For simple conditional filtering, which node should you use first?

    2. How do you deduplicate items by a field?

    Pass the quiz above to unlock lesson completion