score:4

Accepted answer

I see two problems in your data constant. The first one, the .map method returns an array, so there is no need to wrap that value inside an array keys []. The other problem is that you are wrapping the .map return state object in array keys too, that is why the error of creating an object is displayed, because you are returning arrays inside the main mapped array [[{ name: ... }], [{ name: ... }]].

So basically the solution for your issue would be:

const data = posts.map(post => ({name: 'post.firstname', dateregistered: 'post.date', department: 'post.department'})) 

Where the parenthesis allows the map method to directly return the object.

score:1

The issue is that data is being created as an array in an array in an array. This is why when you try const data = posts.map(post => ...) the error persists, as there is still an array in an array. After you try the above, also re-write

post => [{name: 'post.firstname', dateregistered: 'post.date', department: 'post.department'}]

to

post => ({name: 'post.firstname', dateregistered: 'post.date', department: 'post.department'})

(Changing the square brackets to round)

P.S When you map over posts to build the data, you build a object with the the key "name" and the value "'post.firstname'". That value is a string literal and not accessing some other JS object (It will make "post.firstname" the value for all the post's. Same goes for the other keys.


Related Query

More Query from same tag