I'm converting an object into an array but TypeScript throws and error:
"Property 'map' does not exist on type 'unknown'"
(Yes, I googled that error but nothing very clear on the web)
In this live demo you will not see the TypeScript error but in my Angular app I can see it and it's causing issues.
How can I convert this object to the same array shown on my live demo using Lodash or JavaScript?
This is the code:
dataObject = {
A: [
{id: 1, name: 'Mike', lastname: 'asmith'},
{id: 2, name: 'Mary', lastname: 'alters'},
{id: 3, name: 'Boris', lastname: 'al'}
],
B: [
{id: 1, name: 'Lauren', lastname: 'bmith'},
{id: 2, name: 'Travis', lastname: 'balters'},
{id: 3, name: 'Barn', lastname: 'bl'}
],
...
}
dataObjectDisp = Object.entries(this.dataObject).reduce((prev, [key, value]) =>{
return [...prev, ...value.map((user, index) => ({
...user, parent: key, index }))]
}, []);
This is the output that I want:
[
{
"id": 1,
"name": "Mike",
"lastname": "asmith",
"parent": "A",
"index": 0
},
{
"id": 2,
"name": "Mary",
"lastname": "alters",
"parent": "A",
"index": 1
},
...
]
value. Change[key, value]to something like[key, value]: [string, Array<{ id: number; name: string; lastname: string; }>]