You pulled 300 items from an API or a spreadsheet. Some are duplicates, some you don't care about, and for your email you only need the 5 newest. Before you reach for the Code node, check out four simple nodes that organize your list of items.
- Filter: lets through only the items that meet a condition, e.g.
{{ $json.status }}is equal to “paid”. Everything else drops off the list. Unlike IF, it has a single output, so you don't build an empty “false” branch. - Remove Duplicates: removes repeats. In the Compare field you choose All Fields, All Fields Except, or Selected Fields. The last option lets you treat two items with the same field, e.g. email, as duplicates.
- Sort: orders items by a field. In Fields To Sort By you enter the field name, e.g. createdAt, and the direction: Ascending or Descending.
- Limit: keeps only part of the list. You set Max Items, e.g. 5, and in Keep you choose First Items or Last Items.
Watch out for:
- Limit doesn't sort. It takes the first items in the order they arrived. “The 5 newest” means Sort (Descending) first, then Limit.
- Remove Duplicates with Compare: All Fields will let “the same” record through if it differs by as little as the fetch date. On top of that, “Jan@firma.pl” and “jan@firma.pl” are two different strings to n8n. Compare by the field that actually identifies the record, and normalize the letter case beforehand in Edit Fields (Set):
{{ $json.email.toLowerCase() }}. - When Filter rejects everything, 0 items flow on and the next nodes won't run (remember episode 04?).
Rule of thumb: filter and remove duplicates first, then sort, and trim with Limit at the end.
Upcoming meetups and workshops are listed in the Events section.