Skip to content

useObjectFromEntries (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"useObjectFromEntries": "error"
}
}
}
}

Enforce using Object.fromEntries() to build an object from a list of key-value pairs.

Object.fromEntries() turns a list of [key, value] pairs into an object in a single call. Building the same object with Array#reduce(), a for...of loop, or a helper function such as Lodash’s _.fromPairs() takes more code and hides what the code is doing. Spreading the previous object on each step of reduce() also copies the whole object every time, which gets slow for long lists.

This rule reports:

  • reduce() calls that start from {} and add one property on each step, either with object spread or with Object.assign();
  • for...of loops that only copy each pair of a list into an object declared as {} right before the loop;
  • calls to _.fromPairs(), lodash.fromPairs(), and the functions listed in the functions option.

reduce() calls on typed arrays, such as new Uint8Array(values), aren’t reported, because a typed array can’t hold key-value pairs.

const object = pairs.reduce((object, [key, value]) => ({ ...object, [key]: value }), {});
code-block.js:1:22 lint/nursery/useObjectFromEntries  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ This reduce() call builds an object one property at a time.

> 1 │ const object = pairs.reduce((object, [key, value]) => ({ ...object, [key]: value }), {});
│ ^^^^^^
2 │

ℹ Object.fromEntries() builds the same object from a list of [key, value] pairs in a single step, which is shorter and easier to read.

ℹ Turn each item into a [key, value] pair with map(), then pass the result to Object.fromEntries().

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Use Object.fromEntries() instead.

1 │ - const·object·=·pairs.reduce((object,·[key,·value])·=>·({·...object,·[key]:·value·}),·{});
1 │ + const·object·=·Object.fromEntries(pairs.map(([key,·value])·=>·[key,·value]));
2 2 │

const object = pairs.reduce((object, [key, value]) => Object.assign(object, { [key]: value }), {});
code-block.js:1:22 lint/nursery/useObjectFromEntries  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ This reduce() call builds an object one property at a time.

> 1 │ const object = pairs.reduce((object, [key, value]) => Object.assign(object, { [key]: value }), {});
│ ^^^^^^
2 │

ℹ Object.fromEntries() builds the same object from a list of [key, value] pairs in a single step, which is shorter and easier to read.

ℹ Turn each item into a [key, value] pair with map(), then pass the result to Object.fromEntries().

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Use Object.fromEntries() instead.

1 │ - const·object·=·pairs.reduce((object,·[key,·value])·=>·Object.assign(object,·{·[key]:·value·}),·{});
1 │ + const·object·=·Object.fromEntries(pairs.map(([key,·value])·=>·[key,·value]));
2 2 │

const object = {};
for (const [key, value] of pairs) {
object[key] = value;
}
code-block.js:2:1 lint/nursery/useObjectFromEntries  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ This for...of loop builds an object one property at a time.

1 │ const object = {};
> 2 │ for (const [key, value] of pairs) {
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 3 │ object[key] = value;
> 4 │ }
│ ^
5 │

ℹ Object.fromEntries() builds the same object from a list of [key, value] pairs in a single step, which is shorter and easier to read.

ℹ Pass the list of pairs to Object.fromEntries() instead of looping over it.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Use Object.fromEntries() instead.

1 │ - const·object·=·{};
2 │ - for·(const·[key,·value]·of·pairs)·{
3 │ - ····object[key]·=·value;
4 │ - }
1 │ + const·object·=·Object.fromEntries(pairs);
5 2 │

const object = _.fromPairs(pairs);
code-block.js:1:16 lint/nursery/useObjectFromEntries  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ This call to _.fromPairs() builds an object from a list of key-value pairs.

> 1 │ const object = _.fromPairs(pairs);
│ ^^^^^^^^^^^
2 │

ℹ Object.fromEntries() does the same thing and is built into JavaScript, so no helper function is needed.

ℹ Call Object.fromEntries() instead.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Use Object.fromEntries() instead.

1 │ - const·object·=·_.fromPairs(pairs);
1 │ + const·object·=·Object.fromEntries(pairs);
2 2 │

const object = Object.fromEntries(pairs);
// `Object.fromEntries()` always creates an object that inherits from `Object.prototype`.
const dictionary = pairs.reduce((object, [key, value]) => ({ ...object, [key]: value }), Object.create(null));

Type: string[]

Default: []

Other functions that build an object from a list of key-value pairs. A name can contain dots, such as utils.fromPairs, to match a function that is accessed through an object. _.fromPairs() and lodash.fromPairs() are always reported.

biome.json
{
"linter": {
"rules": {
"nursery": {
"useObjectFromEntries": {
"level": "on",
"options": {
"functions": [
"utils.fromPairs"
]
}
}
}
}
}
}
const object = utils.fromPairs(pairs);
code-block.js:1:16 lint/nursery/useObjectFromEntries  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ This call to utils.fromPairs() builds an object from a list of key-value pairs.

> 1 │ const object = utils.fromPairs(pairs);
│ ^^^^^^^^^^^^^^^
2 │

ℹ Object.fromEntries() does the same thing and is built into JavaScript, so no helper function is needed.

ℹ Call Object.fromEntries() instead.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Use Object.fromEntries() instead.

1 │ - const·object·=·utils.fromPairs(pairs);
1 │ + const·object·=·Object.fromEntries(pairs);
2 2 │

const object = Object.fromEntries(pairs);