1

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

3

2 Answers 2

2

 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);

Sign up to request clarification or add additional context in comments.

Comments

0

you can iterate through the objects and add push them to array by this and working example is in the snippet

    let resultArr = [];
    for(var key in state.posts.byId){
        resultArr.push(state.posts.byId[key]);
    }

 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"]
        }
    
      }


    let resultArr = [];
    for(var key in state.posts.byId){
        resultArr.push(state.posts.byId[key]);
    }
    console.log(resultArr);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.