You likely want to use .map
here instead of .forEach
:
const [itemsWithSelection, setItemsWithSelection] = useState<{ [formFieldId: string]: number }>(itemsWithSelection2)
if (!itemsWithSelection2) {
let aa = Object.keys(invoiceItems).map((key) =>
[key, 1]
)
setItemsWithSelection(Object.fromEntries(aa))
}
The first produces a new array based on the contents of the callback (or what is often called “closure” in Swift); while the second is used primarily for side effects, and always returns undefined
(a value similar to Swift’s Void
).
solved TypeScript: Why Object.fromEntries does not accept array of tuple? [duplicate]