n8n LabIntermediate
Find One Object in an Array with find() in n8n
You have a list of agents and need the email of the agent with id 2 to assign a new lead.
Live Execution Simulator
Idle
π₯
Incoming Data
βοΈ
n8n Node
π―
Next Step
> Ready. Type your expression below and click "Check Answer" to run the node.
Incoming Payload / Data:
{
"agents": [
{ "id": 1, "email": "[email protected]" },
{ "id": 2, "email": "[email protected]" }
]
}
$json.agents.find(a =>
).email
βοΈ Your expression is executed against test data β any correct solution passes. +20 XP
β Solution & explanation
$json.agents.find(a => a.id === 2).email
Output: [email protected]
find() returns the first matching element (not an array), so you can read .email directly. It returns undefined if nothing matches.π‘ Pro tip: Guard against no match: $json.agents.find(a => a.id === 2)?.email ?? 'unassigned'.
Also accepted: a.id == 2