Files
it-know-how/typescript/bits/04-array-find-and-arrow-functions.md
2026-03-27 12:58:01 +01:00

710 B
Executable File

Array find and Arrow Functions

Array.prototype.find returns the first element that matches a condition.

Example

const parts = [
	{ type: 'year', value: '2026' },
	{ type: 'month', value: '03' },
	{ type: 'day', value: '24' },
];

const yearPart = parts.find((part) => part.type === 'year');
console.log(yearPart); // { type: 'year', value: '2026' }

Arrow Function Syntax

(part) => part.type === 'year'
  • (part): parameter.
  • =>: arrow token.
  • part.type === 'year': expression result (true or false).

If no item matches, find returns undefined.

Python Comparison

Similar to:

year_part = next((p for p in parts if p['type'] == 'year'), None)