嗨,我有一个redux状态作为对象
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"]
}
}
我希望它将其转换为数组,以便可以在React中使用它。它看起来像
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:[]
}
]
怎么做 我的尝试不起作用
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)
// }
))
另一种尝试不起作用
const postOrder = state.posts.allIds
const a = {posts:postOrder.map((post)=>(
{
post,
Object.keys(state.posts.byId.reduce(()=>{
}))
}
))}
现在,我尝试搜索互联网,发现有些人使用loaddash库,甚至使用reduce方法,但是自从
我不是JavaScript开发人员
,这对我来说很难。我按行业是python开发人员
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"]
}
}
const posts = Object.values(state.posts.byId)
console.log(posts);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句