useObjectFromEntries (JavaScript)
Summary
Section titled “Summary”- Rule available since:
v2.6.5 - Diagnostic Category:
lint/nursery/useObjectFromEntries - This rule has an unsafe fix.
- The default severity of this rule is warning.
- Sources:
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "useObjectFromEntries": "error" } } }}Description
Section titled “Description”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 withObject.assign();for...ofloops 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 thefunctionsoption.
reduce() calls on typed arrays, such as new Uint8Array(values), aren’t reported,
because a typed array can’t hold key-value pairs.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”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));Options
Section titled “Options”functions
Section titled “functions”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.
{ "linter": { "rules": { "nursery": { "useObjectFromEntries": { "level": "on", "options": { "functions": [ "utils.fromPairs" ] } } } } }}Invalid
Section titled “Invalid”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);Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.