Hi I have a redux state as an object
const state= {
posts: {
byId:{
"8xf0y6ziyjabvozdd253nd":{
id: '8xf0y6ziyjabvozdd253nd',
timestamp: 1467166872634,
title: 'Udacity is the best place to learn React',
body: 'Everyone says so after all.',
author: 'thingtwo',
category: 'react',
voteScore: 6,
deleted: false,
commentCount: 2,
comments: ["894tuq4ut84ut8v4t8wun89g", "8tu4bsun805n8un48ve89"]
},
"6ni6ok3ym7mf1p33lnez":{
id: '6ni6ok3ym7mf1p33lnez',
timestamp: 1468479767190,
title: 'Learn Redux in 10 minutes!',
body: 'Just kidding. It takes more than 10 minutes to learn technology.',
author: 'thingone',
category: 'redux',
voteScore: -5,
deleted: false,
commentCount: 0,
comments:[]
}
},
allIds:["8xf0y6ziyjabvozdd253nd","6ni6ok3ym7mf1p33lnez"]
}
}
I want it to convert it to an Array so I can use it in React. It should look like
posts: [
{
id: '8xf0y6ziyjabvozdd253nd',
timestamp: 1467166872634,
title: 'Udacity is the best place to learn React',
body: 'Everyone says so after all.',
author: 'thingtwo',
category: 'react',
voteScore: 6,
deleted: false,
commentCount: 2,
comments: ["894tuq4ut84ut8v4t8wun89g", "8tu4bsun805n8un48ve89"]
},
{
id: '6ni6ok3ym7mf1p33lnez',
timestamp: 1468479767190,
title: 'Learn Redux in 10 minutes!',
body: 'Just kidding. It takes more than 10 minutes to learn technology.',
author: 'thingone',
category: 'redux',
voteScore: -5,
deleted: false,
commentCount: 0,
comments:[]
}
]
How do it? My Try that does not work
const postsList = state.posts.byId.map(post=>(
{
id: post.id,
timestamp: post.timestamp,
title: post.title,
body: post.body,
author: post.author,
category: post.category,
voteScore: post.voteScore,
deleted: post.deleted,
commentCount:post.commentCount,
comments: post.comments.map(id=>id)
// }
))
Another try that does not work
const postOrder = state.posts.allIds
const a = {posts:postOrder.map((post)=>(
{
post,
Object.keys(state.posts.byId.reduce(()=>{
}))
}
))}
Now I have tried searching the internet I have seen some people using loaddash library, and some even using the reduce method but since
I am not a javascript developer
, this is all tough for me. I am a python developer by trade