I am doing a loop on an Object using forEach
or map
.
However, I am getting a type error on the variable that I use to loop.
The error is Object is of type 'unknown'.
This occurs on val.name
, val.title
.
With .forEach
Object.entries(data).forEach(
([, val]) => {
newObject[val.name] = { // Object is of type 'unknown'
title: val.title // Object is of type 'unknown'
}}
)
With .map
Object.entries(data).map(
([, val]) => {
newObject[val.name] = { // Object is of type 'unknown'
title: val.title // Object is of type 'unknown'
}}
)
I tried to specify a type on the variable val
such as:
([, val: any]) => {..my code..}
but it does not accept a type in that position
I found the same problem, finally I just replace [k, v] by (k, v: any)
Object.entries(obj).forEach((key, value: any) => {console.log(key, " => ", value);});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With