Filter the elements according to another value

前端 未结 2 1785
慢半拍i
慢半拍i 2021-01-27 13:33

I want to output the number of all questions for each post in ReactJs. For this i created the next code:

2条回答
  •  南方客
    南方客 (楼主)
    2021-01-27 13:54

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

提交回复
热议问题