Skip to main content

property()

property<Key>(key): <T>(object) => T[Key]

Creates a function that returns the value at path of a given object.

DEPRECATED

Use an inline arrow function (obj) => obj[key] instead.


Type Parameters​

Key: Key extends PropertyKey​

The type of the key.


Parameters​

key: Key​

The key of the property to get.


Returns: <T>(object): T[Key]​

A function that returns the property value.


Since​

2.0.0


Also known as​

prop (Remeda, Ramda) · property (Lodash, es-toolkit) · ❌ (Radashi, Effect, Modern Dash, Antfu)


Example​

// ❌ Deprecated approach
const users = [{ name: 'fred' }, { name: 'barney' }];
users.map(property('name'));
// => ['fred', 'barney']

// βœ… Recommended approach
const users = [{ name: 'fred' }, { name: 'barney' }];
users.map(user => user.name);
// => ['fred', 'barney']

How it works?​

Creates a function that returns the value at path of a given object. Deprecated: Use an arrow function.

Native Equivalent​

// ❌ property('name')
// βœ… obj => obj.name
// βœ… obj => obj?.nested?.value

Use Cases​

Get property accessor πŸ“Œβ€‹

Create property accessor function.

const getName = (obj) => obj.name;
users.map(getName);
// => ["Alice", "Bob"]

Dynamic property access​

Access property by variable key.

const get = (key) => (obj) => obj[key];
const getAge = get("age");
users.map(getAge);

Pluck values​

Extract property from objects.

items.map(item => item.id);