简体   繁体   English

使用反应路由器按ID过滤项目返回空数组

[英]filter item by id using react router return empty array

I am using react router...route path: /product/:id 我正在使用React Router ...路由路径:/ product /:id

I am trying to filter item by id to that matches with url but it's returning nothing...could you guys help me? 我正在尝试按id过滤与url匹配的项,但是它什么也没返回...你们能帮我吗?

import React from 'react'


function SelectedProduct(props) {

  const { id } = props.match.params
  console.log(id) 
  console.log(props.product)

  const filtered = props.product.filter(product => product.id === props.match.params.id)
  console.log(filtered)

  return (
    <div>

    </div>
  )
}

export default SelectedProduct

Console.log image Console.log映像

May be the id in your number is an integer and you are equalizing it to a string? 可能您的数字中的id是一个整数,并且正在将其均衡为字符串? Can you try the below code? 您可以尝试以下代码吗?

const filtered = props.product.filter(product => parseInt(product.id, 10) === parseInt(props.match.params.id, 10))
console.log(filtered)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM