Javascript Can';在REACT/REDUX中,无法获取正确更新对象内部数组的状态

Javascript Can';在REACT/REDUX中,无法获取正确更新对象内部数组的状态,javascript,reactjs,redux,lodash,Javascript,Reactjs,Redux,Lodash,我将一步一步地把这些分解为我想要发生的事情,希望人们能理解我想要什么 使用React/Redux、Lodash 我有许多帖子是作为一个数组从后端api发送的。每个帖子都有一个\u id。当我调用actiongetAllPost()时,它会返回包含所有post的数组。这很好用 然后我分派typeGET_ALL_POSTS,它触发reducerreducer_POSTS来更改/更新状态 减速器: export default function(state = {}, action) { swit

我将一步一步地把这些分解为我想要发生的事情,希望人们能理解我想要什么

使用React/Redux、Lodash

  • 我有许多帖子是作为一个数组从后端api发送的。每个帖子都有一个
    \u id
    。当我调用action
    getAllPost()
    时,它会返回包含所有post的数组。这很好用

  • 然后我分派type
    GET_ALL_POSTS
    ,它触发reducer
    reducer_POSTS
    来更改/更新状态

  • 减速器:

    export default function(state = {}, action) {
      switch(action.type) {
        case GET_ALL_POSTS:
        const postsState =  _.mapKeys(action.payload.data, '_id');
        //const newPostsState = _.map(postsState, post => {
          //const newComments = _.mapKeys(post.comments, '_id');
        //});
          return postsState;
        break;
        default:
          return state;
        break;
      }
    }
    
  • 如您所见,我将数组更改为一个巨大的对象,其中包含许多post-As对象,这些对象的键与其“_-id”相等。这样做很好,把这部分州还回去也很好
  • 正如我提到的,这些帖子中的每一篇都有一个comments值,它是一个数组。我想将comments数组更改为一个大对象,将每个注释作为一个对象保存,其键等于它们的“_id”,就像我在文章中所做的那样
  • 现在我需要一次完成这一切,并返回新创建的状态,其中有一个大对象,包含所有post as对象,并且在每个post上都应该有一个comments对象,包含所有comments as对象。我将尝试编写一些示例代码来说明我正在尝试做什么
  • 例如:

    BigPostsObject { 
    1: SinglePostObject{}, 
    2: SinglePostObject{}, 
    3: SinglePostObject {
      _id: '3',
      author: 'Mike',
      comments: BigCommentObject{1: SingleCommentObject{}, 2: SingleCommentObject{}}
     }
    }
    

    我希望这个例子能澄清我想做的事情。如果对我正在做的事情仍然感到困惑,那么请询问,也请不要说像使用数组之类的话。我知道我可以使用数组,但这对本文没有帮助,好像其他人想这样做一样,这是没有帮助的信息。

    我相信现在的JS内置函数可以做到这一点,而不需要外部库。无论如何,这应该是一条路。我真的鼓励你回到js内置函数

    var data = [
     {
      _id: '3',
      title: 'Going on vaccation',
      comments:[ 
        {_id: 1, comment: 'hello'},
        {_id: 2, comment: 'world'}           
      ] 
     }, 
    
     {
      _id: '2',
      title: 'Going to dinner',
      comments:[ 
        {_id: 1, comment: 'hello'},
        {_id: 2, comment: 'world'}           
      ] 
     } 
    
    ]
    
    //you can use JS builtin reduce for this
    var transformedPost= _.reduce(data, function(posts, post) {
      var newPost = Object.assign({}, post)
      newPost._id=post._id
      //you can use js builtin map for this 
      newPost.comments = _.mapKeys(post.comments, '_id')
    
      // if you are using es6, replace the last three line with this 
      //return Object.assign({}, posts, {[newPost._id]: newPost})
    
      var item = {}
      item[newPost._id]=newPost
      return Object.assign({}, posts, item)
    },{});
    
    console.log(transformedPost)
    

    编写一个函数,用于处理评论数组中每个帖子的所有评论:

    function processComment(post) {
       post.bigCommentsObject = _.mapKeys(post.comments, '_id');
       // now the comments array is no longer needed
       return _.omit(post, ['comments']);
    
    }
    
    现在,使用该函数将每个注释数组转换为一个大对象,其中包含所有注释,而注释仍在数组中。然后在一个大对象中旋转阵列本身:

    const commentsProcessed =  _.map(action.payload.data, procesComment);
    const postsState =  _.mapKeys(commentsProcessed, '_id');   
    

    为什么在这里使用reduce?reduce是一种在转换最终数据时循环数组的方法。在这种情况下,它返回单个输出(对象)。请注意,我传递给回调的
    posts
    参数是一个累加器,它将是您最后转换的post。您如何使用纯js复制它?而不是用lodash?对不起。我已经离开了。这是一个使用纯js的js库的更新,正如您所看到的,它很冗长,但js可以做到这一点。我相信随着时间的推移,这些数据处理库将逐渐消失。尽管如此,它们仍然有一些用例