Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在Redux中将对象转换为数组_Javascript_Reactjs_Redux_React Redux - Fatal编程技术网

Javascript 在Redux中将对象转换为数组

Javascript 在Redux中将对象转换为数组,javascript,reactjs,redux,react-redux,Javascript,Reactjs,Redux,React Redux,嗨,我有一个redux状态作为对象 const state= { posts: { byId:{ "8xf0y6ziyjabvozdd253nd":{ id: '8xf0y6ziyjabvozdd253nd', timestamp: 1467166872634, title: 'Udacity is the best place to learn Re

嗨,我有一个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(()=>{

                }))
            }
        ))}
另一次尝试不起作用

        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开发者,您可以迭代对象并将其添加到数组中 通过这个示例,代码段中有一个有效的示例

    let resultArr = [];
    for(var key in state.posts.byId){
        resultArr.push(state.posts.byId[key]);
    }
const state={
职位:{
byId:{
“8xf0y6ziyjabvozdd253nd”:{
id:'8xf0y6ziyjabvozdd253nd',
时间戳:146716872634,
标题:“Udacity是学习反应的最佳场所”,
身体:“毕竟每个人都这么说。”,
作者:《thingtwo》,
类别:“反应”,
voteScore:6,
删除:false,
总数:2,
注释:[“894tuq4ut84ut8v4t8wun89g”,“8tu4bsun805n8un48ve89”]
},
“6Ni6OK3YM7MF13LNEZ”:{
id:'6NI6OK3YM7MF13LNEZ',
时间戳:14684767190,
标题:“在10分钟内学习Redux!”,
body:“开玩笑。学习技术需要10分钟以上。”,
作者:“thingone”,
类别:'redux',
voteScore:-5,
删除:false,
评论计数:0,
评论:[]
}
},
所有符号:[“8xf0y6ziyjabvozdd253nd”,“6ni6ok3ym7mf1p33lnez”]
}
}
设resultar=[];
for(state.posts.byId中的var键){
resultar.push(state.posts.byId[key]);
}
console.log(resultar)
const state={
职位:{
byId:{
“8xf0y6ziyjabvozdd253nd”:{
id:'8xf0y6ziyjabvozdd253nd',
时间戳:146716872634,
标题:“Udacity是学习反应的最佳场所”,
身体:“毕竟每个人都这么说。”,
作者:《thingtwo》,
类别:“反应”,
voteScore:6,
删除:false,
总数:2,
注释:[“894tuq4ut84ut8v4t8wun89g”,“8tu4bsun805n8un48ve89”]
},
“6Ni6OK3YM7MF13LNEZ”:{
id:'6NI6OK3YM7MF13LNEZ',
时间戳:14684767190,
标题:“在10分钟内学习Redux!”,
body:“开玩笑。学习技术需要10分钟以上。”,
作者:“thingone”,
类别:'redux',
voteScore:-5,
删除:false,
评论计数:0,
评论:[]
}
},
所有符号:[“8xf0y6ziyjabvozdd253nd”,“6ni6ok3ym7mf1p33lnez”]
}
}
const posts=Object.values(state.posts.byId)

控制台日志(posts)
,然后对结果使用
.map()
。可能的重复不是重复,因为查找的数组不同。