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.
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.
@param ― callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.
@param ― initialValue 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.
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.
@param ― callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.
@param ― initialValue 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;
} (+3overloads)
Copy the values of all of the enumerable own properties from one or more source objects to a
target object. Returns the target object.
@param ― target The target object to copy to.
@param ― source The source object from which to copy properties.
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.
@param ― callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.
@param ― initialValue 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.
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.
@param ― callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.
@param ― initialValue 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 }),
{},
);
const
const entries:(string|number)[][]
entries = [
["first", 1],
["second", 2],
];
const
const result:any
result =
var Object:ObjectConstructor
Provides functionality common to all JavaScript objects.
Object.
ObjectConstructor.fromEntries(entries: Iterable<readonly any[]>): any (+1 overload)
Returns an object created by key-value entries for properties and methods
@param ― entries An iterable object that contains key-value entries for properties and methods.
fromEntries(
const entries:(string|number)[][]
entries);
const
const entries:(string|number)[][]
entries = [
["first", 1],
["second", 2],
];
const
const result: {
[k:string]:number;
}
result =
var Object:ObjectConstructor
Provides functionality common to all JavaScript objects.
Calls a defined callback function on each element of an array, and returns an array that contains the results.
@param ― callbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array.
@param ― thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
map(([
key: string | number
key,
value: string | number
value]) => [
key: string | number
key, value*2]),
Error ts(2362) ― The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
);
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: {
[k:string]:string;
}
lookup =
var Object:ObjectConstructor
Provides functionality common to all JavaScript objects.
Calls a defined callback function on each element of an array, and returns an array that contains the results.
@param ― callbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array.
@param ― thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
map((
item: {
id: string;
name: string;
}
item) => [
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.
Calls a defined callback function on each element of an array, and returns an array that contains the results.
@param ― callbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array.
@param ― thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
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.