【问题标题】:Filter the elements according to another value根据另一个值过滤元素
【发布时间】:2020-03-24 17:19:22
【问题描述】:

我想在 ReactJs 中输出每个帖子的所有问题的数量。 为此,我创建了下一个代码:

const posts = [{
        title: 1,
        id: "123"
    },
    {
        title: 2,
        id: "1234"
    },
    {
        title: 3,
        id: "12345"
    }
]

const questions = [{
        id: 55,
        name: 'question one',
        id_post: '123'
    },
    {
        id: 56,
        name: 'question two',
        id_post: '123'
    },
    {
        id: 57,
        name: 'question three',
        id_post: '1234'
    },
    {
        id: 58,
        name: 'question four',
        id_post: '123'
    },

];

posts.map( (e, k) => {
    return (
      <Link key={k} to={`demo/${e.id}/url`}>
      { questions.filter(here i want to output the number of questions that correspond to each post)}
      </Link>
    )
})

我有posts 数组和questions 数组。我想在url中创建一个Link,它有自己的id,同时过滤每个帖子的问题数,并在Link内输出数字。这个怎么办?

...问题是下一个:我正在使用 ant design,table 组件,我可以使用下一个结构:

`  render: ()=>(
    console.log('render'),
    events.map( (e, key) => {
      console.log(ev.id);
        return (
            <Link key={k} to={`demo/${e.id}/url`}>
            { questions.filter(q => q.id_post === e.id).length }
            </Link>
        )
      )
    })

I use this to create a column in my table. The problem is that i have to many renders. When i put this code i get all ids inconsole.log(ev.id)on each render. And at the end i get for example not0as length but00000000`,取决于我有多少渲染或ID。如何解决这个问题?请看一下:45 https://codesandbox.io/s/8i1dy

【问题讨论】:

  • 您的 id 是 posts 中的字符串和 questions 中的整数,是否正确,或者您可以修改其中任何一个?
  • @Anurag Srivastava,我更正了。两个 id 都是字符串。

标签: javascript reactjs


【解决方案1】:

一种可能的方法是预先计算:

const questionCountByPost = questions.reduce((acc, q) => {
  const postId = q.id_post;
  acc[postId] = (acc[postId] || 0) + 1;
  return acc;
}, {});

...每当您的帖子或问题发生变化时,这看起来都是一件好事。您可以像这样在地图函数中使用此对象:

return (
  <Link key={k} to={`demo/${e.id}/url`}>
  { questionCountByPost[e.id] }
  </Link>
)

另一种方法是直接在模板中进行计数:

return (
  <Link key={k} to={`demo/${e.id}/url`}>
  { questions.filter(q => q.id_post === e.id).length }
  </Link>
)

它的性能较低(因为您每次都必须遍历整个数组),但显然更具可读性。如果帖子和问题的数量不是那么大,它可能是一个更好的解决方案。

【讨论】:

    【解决方案2】:

    const posts = [{
            title: 1,
            id: "123"
        },
        {
            title: 2,
            id: "1234"
        },
        {
            title: 3,
            id: "12345"
        }
    ]
    
    const questions = [{
            id: 55,
            name: 'question one',
            id_post: 123
        },
        {
            id: 56,
            name: 'question two',
            id_post: 123
        },
        {
            id: 57,
            name: 'question three',
            id_post: 1234
        },
        {
            id: 58,
            name: 'question four',
            id_post: 123
        },
    
    ];
    
    
    posts.map(({id}) => {
      let count = 0
      console.log(questions.filter(({id_post}) => (id_post == id)).length)
      
    })

    【讨论】:

    • 你可以使用filter + Array.length
    • @AnuragSrivastava 是的,这也是一种选择。
    猜你喜欢
    • 2018-07-14
    • 1970-01-01
    • 1970-01-01
    • 2015-11-03
    • 2022-10-14
    • 1970-01-01
    • 2021-04-04
    • 1970-01-01
    • 2012-01-29
    相关资源
    最近更新 更多