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

steve
  • 25
  • 5
  • 3
    [`Object.keys()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys), then use `.map()` on the result. – Matt Way Apr 17 '18 at 12:50
  • 2
    Possible duplicate of [convert Object {} to array \[\] in javascript](https://stackoverflow.com/questions/38824349/convert-object-to-array-in-javascript) – jmargolisvt Apr 17 '18 at 13:01
  • Not a duplicate, as the looked for array is different. – Katinka Hesselink Oct 03 '19 at 08:35

2 Answers2

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);
Mayur Babaria
  • 56
  • 1
  • 2
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);
mooga
  • 3,136
  • 4
  • 23
  • 38