Skip to content

objectEntriesMethods

Prefer Object.fromEntries() over reduce patterns that build objects from key-value pairs.

✅ This rule is included in the ts stylistic and stylisticStrict presets.

Object.fromEntries() is a built-in method that transforms a list of key-value pairs into an object. It provides a more readable and concise alternative to using reduce() to build objects from entries.

Using Object.fromEntries() with map() is clearer than the equivalent reduce pattern, and avoids creating unnecessary intermediate objects on each iteration.

const
const entries: (string | number)[][]
entries
= [
["first", 1],
["second", 2],
];
const
const result: {}
result
=
const entries: (string | number)[][]
entries
.
Array<(string | number)[]>.reduce<{}>(callbackfn: (previousValue: {}, currentValue: (string | number)[], currentIndex: number, array: (string | number)[][]) => {}, initialValue: {}): {} (+2 overloads)

Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.

@paramcallbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.

@paraminitialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.

reduce
(
(
accumulator: {}
accumulator
, [
key: string | number
key
,
value: string | number
value
]) => ({ ...
accumulator: {}
accumulator
, [
key: string | number
key
]:
value: string | number
value
}),
{},
);
const
const entries: (string | number)[][]
entries
= [
["first", 1],
["second", 2],
];
const
const result: {}
result
=
const entries: (string | number)[][]
entries
.
Array<(string | number)[]>.reduce<{}>(callbackfn: (previousValue: {}, currentValue: (string | number)[], currentIndex: number, array: (string | number)[][]) => {}, initialValue: {}): {} (+2 overloads)

Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.

@paramcallbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.

@paraminitialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.

reduce
(
(
accumulator: {}
accumulator
, [
key: string | number
key
,
value: string | number
value
]) =>
var Object: ObjectConstructor

Provides functionality common to all JavaScript objects.

Object
.
ObjectConstructor.assign<{}, {
[x: string]: string | number;
}>(target: {}, source: {
[x: string]: string | number;
}): {
[x: string]: string | number;
} (+3 overloads)

Copy the values of all of the enumerable own properties from one or more source objects to a target object. Returns the target object.

@paramtarget The target object to copy to.

@paramsource The source object from which to copy properties.

assign
(
accumulator: {}
accumulator
, { [
key: string | number
key
]:
value: string | number
value
}),
{},
);
const
const items: {
id: string;
name: string;
}[]
items
= [
{
id: string
id
: "a",
name: string
name
: "Alpha" },
{
id: string
id
: "b",
name: string
name
: "Beta" },
];
const
const lookup: {}
lookup
=
const items: {
id: string;
name: string;
}[]
items
.
Array<{ id: string; name: string; }>.reduce<{}>(callbackfn: (previousValue: {}, currentValue: {
id: string;
name: string;
}, currentIndex: number, array: {
id: string;
name: string;
}[]) => {}, initialValue: {}): {} (+2 overloads)

Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.

@paramcallbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.

@paraminitialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.

reduce
(
(
accumulator: {}
accumulator
,
item: {
id: string;
name: string;
}
item
) => ({ ...
accumulator: {}
accumulator
, [
item: {
id: string;
name: string;
}
item
.
id: string
id
]:
item: {
id: string;
name: string;
}
item
.
name: string
name
}),
{},
);
const
const entries: [string, number][]
entries
=
var Object: ObjectConstructor

Provides functionality common to all JavaScript objects.

Object
.
ObjectConstructor.entries<number>(o: {
[s: string]: number;
} | ArrayLike<number>): [string, number][] (+1 overload)

Returns an array of key/values of the enumerable own properties of an object

@paramo Object that contains the properties and methods. This can be an object that you created or an existing Document Object Model (DOM) object.

entries
({
first: number
first
: 1,
second: number
second
: 2 });
const
const doubled: {}
doubled
=
const entries: [string, number][]
entries
.
Array<[string, number]>.reduce<{}>(callbackfn: (previousValue: {}, currentValue: [string, number], currentIndex: number, array: [string, number][]) => {}, initialValue: {}): {} (+2 overloads)

Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.

@paramcallbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.

@paraminitialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.

reduce
(
(
accumulator: {}
accumulator
, [
key: string
key
,
value: number
value
]) => ({ ...
accumulator: {}
accumulator
, [
key: string
key
]:
value: number
value
* 2 }),
{},
);

This rule is not configurable.

If you need to support JavaScript environments that don’t have Object.fromEntries() (pre-ES2019), you might need to disable this rule or use a polyfill. Object.fromEntries() is supported in Node.js 12.0.0+ and all modern browsers.

Some reduce patterns are more complex than what this rule detects, such as when the accumulator needs additional processing or when building an object with conditional keys. If you use those patterns often and prefer to keep stylistic consistency with them, you might prefer to disable this rule.

Made with ❤️‍🔥 around the world by the Flint team and contributors.