【问题标题】:Array doesnt display all of the items inside it React JavaScript数组不显示其中的所有项目 React JavaScript
【发布时间】:2020-08-22 19:23:52
【问题描述】:

我根据一些 id 从 API 收到一些信息。有 3 个作业对象,每个作业都有一个标签数组,在每个数组中,有一些 id 被发送到 API 以返回标签信息:

function useJobs () {
  const [jobs, setJobs] = React.useState([])
  const [locations, setLocations] = React.useState({})
  const [departments, setDepartments] = React.useState({})
  const [tags, setTags] = React.useState({})

  React.useEffect(() => {
    async function fetchTags () {
      const tPromises = []
      for (const job of jobs) {
        for (const tag of job.tags) {
          console.log(tag)
          tPromises.push(fetchJSON(`/api/jobs/view-tag/${tag}`, { headers: headers })
            .then((tags) => {
              return { [job.id]: tags }
            }))
        }
      }
      const dData = await Promise.all(tPromises)
      console.log(dData)
      setTags(prev => Object.assign({}, ...dData))
    }
    fetchTags()
  }, [jobs])

  return [jobs, locations, departments, tags]
}
export default function Jobs () {
 const [jobs, locations, departments, tags] = useJobs()
.....
{jobs.map(job => (
   {tags[job.id] && <Col key={tags[job.id].id}>Tags:{tags[job.id].name} </Col>}
)}
....
}

问题是每个作业的每个标签数组中只有一个项目被打印。例如,如果数组是[1,2,3],然后转换为[Css,Js,HTML],则只显示Css。我该如何解决这个问题?

编辑:


  React.useEffect(() => {
    async function fetchTags () {
      const tPromises = []
      for (const job of jobs) {
        for (const tag of job.tags) {
          console.log(tag)
          tPromises.push(fetchJSON(`/api/jobs/view-tag/${tag}`, { headers: headers })
            .then((tags) => {
              return { [job.id]: tags }
            }))
        }
      }
      const dData = await Promise.all(tPromises)
      const tags = dData.reduce((acc, item) => {
        const [key, value] = Object.entries(item)
        if (acc[key]) {
          acc[key].push(value)
        } else {
          acc[key] = [value]
        }
        return acc
      }, {})
      setTags(prev => ({ ...prev, ...tags }))
      console.log(dData)
      setTags(prev => Object.assign({}, ...dData))
    }
    fetchTags()
  }, [jobs])

  return [jobs, locations, departments, tags]
}

【问题讨论】:

    标签: javascript arrays reactjs react-hooks


    【解决方案1】:

    您从 promise.all 收到的数据对于同一个 jobId 将具有多个标签,因此您无法直接合并该对象,而是需要对其进行处理以将其转换为 objectId 到标签数组映射

      const dData = await Promise.all(tPromises)
      const tags = dData.reduce((acc, item) => {
          const [key, value] = Object.entries(item)[0];
          if(acc[key]) {
             acc[key].push(value);
          } else {
             acc[key] = [value];
          }
          return acc;
      }, {})
      setTags(prev => ({...prev, ...tags}))
    

    一旦你这样做了,你就可以映射标签并渲染它们

       {tags[job.id] && tags[job.id].map(tag => <Col key={tags[job.id].id}>Tags:{tag.name} </Col>)}
    

    【讨论】:

    • 我收到此错误:未处理的拒绝 (TypeError): acc is undefined
    • 对不起,我忘记了,return acc; 在 reduce 函数中。更新了帖子
    • Now TypeError: tags[job.id].map is not a function
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-23
    • 1970-01-01
    • 2012-03-21
    • 2021-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多