根据过滤后的数组检索数据

时间:2019-04-26 15:41:37

标签: reactjs redux

我试图过滤基于post.id比较的数组,检索值并将其附加到likes属性。

action.data.filter((post) => post.id === action.id).Likes.length

无法正常运行,给我一个错误,即长度未定义

如果我做类似的事情

 action.data.filter((post) => post.id === 5)

它会给我即时寻找的结果,但这是行不通的,它需要动态。

return {
  ...state, 
  posts: action.data, // maps posts fine,
  likes: action.data.filter((post) => post.id === action.id).Likes.length // does not do what i want it do :(
 }

它应该是动态的,以便该特定帖子的任何值都有其自己的值。我应该怎么做,或者可能是解决方案?

称为{this.props.likes}

action.id (获取现有的帖子ID)

console.log(action.id)

输出

  

[5,2]

发布数据输出

(2) [{…}, {…}]
 0:
  Likes: (32) 
  createdAt: "2019-04-26T09:38:10.324Z"
  id: 5
  post_content: "ssss"
  title: "React Interview Questionsdd"
  updatedAt: "2019-04-26T09:38:10.324Z"
  userId: 1
  username: "owlmans"
  __proto__: Object
 1: {id: 3, title: "React Interview sssQuestions", post_content: "ggg", 
  username: "owlman", createdAt: "2019-04-24T20:48:36.710Z", …}
  length: 2
  __proto__: Array(0)

Actions.js

export const GetPosts = () => {
    return (dispatch, getState) => {
        return Axios.get('/api/posts/myPosts')
            .then( (res) => {
                 const data = res.data
                 const id = data.map( (post) => post.id)  // gets posts id [5,3]
                 dispatch({type: GET_POSTS, data, id})
             })

    }
}

3 个答案:

答案 0 :(得分:0)

过滤器返回过滤数据的数组..因此,当您进行过滤访问时,零索引可检索喜欢的长度

点赞总数:

    action.data.filter((post) => action.id.includes(post.id)).reduce((a,b) => {
     return a.Likes.length + b.Likes.length
    })

对于喜欢的人:

    action.data.filter((post) => action.id.includes(post.id)).map((a) => {
     return { postlikes: a.Likes.length }
    })

有关更多详细信息,请阅读filters

答案 1 :(得分:0)

尝试一下

const filtered = action.data.filter((post) => action.id.includes(post.id))

return filtered.map((post,index) => `post ${index} has ${post.Likes.length} likes`)

答案 2 :(得分:0)

所以您想知道帖子的总赞数吗?或每个帖子的总赞?

因此,如果您想获得喜欢的总和,可以使用“减少”。

在此处检查文档: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

而且看起来并非每个对象都具有Likes porperty,所​​以您必须对其进行检查。

let totalNumberOfLikes = action.data.posts.reduce( acc, post =>{

 if(post.id == action.id && post.hasOwnProperty('Likes'){
    acc += post.Likes.length
 }
} , 0 );


return {
  ...state, 
  posts: action.data,
  likes: totalNumberOfLikes }

如果您想为几乎所有的帖子获得喜欢,请记住filter返回一个数组,因此您正在检查已筛选帖子的数组Likes的属性。

您要做的是检查每个帖子的属性Likes,而不是过滤的帖子数组。

所以代替: action.data.filter((post) => post.id === action.id).Likes.length。 你可以做这样的事情:

let filteredPosts = action.data.posts.filter( post => post.id == action.id );

let totalLikesForEachPost  =  {}; 

//Here you need an object to keep the record of every like in the post if this has any and an id so you can access the data.
filteredPosts.forEach( post => {
  totalLikesForEachPost[ post.id ] = {  
       totalLikes: post.hasOwnProperty('Likes') ? post.Likes.length : 0,
       id: post.id,
  }
} );


return {
  ...state, 
  posts: action.data,
  likes: totalLikesForEachPost }