[英]Accessing a Nested JSON and Iterating Over JS/React
Please see sandbox below请参阅下面的沙箱
https://codesandbox.io/embed/interesting-cloud-d7iff0?fontsize=14&hidenavigation=1&theme=dark https://codesandbox.io/embed/interesting-cloud-d7iff0?fontsize=14&hidenavigation=1&theme=dark
I'm making an API request to a database any pulling through job results.我正在通过工作结果向数据库发出 API 请求。 The data is nested quite significantly, please see example: https://gyazo.com/c91a633b588088ae1388a4bbba14554f
数据嵌套非常明显,请参见示例: https ://gyazo.com/c91a633b588088ae1388a4bbba14554f
Within this, results is an array of all 10, which is not structured.其中,results 是一个包含所有 10 个的数组,它不是结构化的。 Therefore, when it is listed to the console.log, it is like the following: https://gyazo.com/cf84ed56c9ba07e10d0a4fc0a894d545
所以列到console.log的时候是这样的: https ://gyazo.com/cf84ed56c9ba07e10d0a4fc0a894d545
Therefore, I am trying to iterate over these, and access for example, a specific ID of all 10 returned.因此,我试图遍历这些,并访问例如返回的所有 10 个特定 ID。 I have a UseEffect Hook like this:
我有一个这样的 UseEffect Hook:
function reducer(state, action) {
switch (action.type) {
case ACTIONS.MAKE_REQUEST:
return { loading: true, jobs: [] }
case ACTIONS.GET_DATA:
return { ...state, loading: false, jobs: action.payload.jobs.results}
case ACTIONS.ERROR:
return { ...state, loading: false, error: action.payload.error, jobs: [] }
case ACTIONS.UPDATE_HAS_NEXT_PAGE:
return { ...state, hasNextPage: action.payload.hasNextPage }
default:
return state
}
}
export default function useFetchJobs(params, page){
//set initial state for reducer
const [state, dispatch] = useReducer(reducer, {jobs: [], loading: true })
//whenever params change, update here, array set to adjustable features
useEffect(() => {
//API canellations
const cancelToken = axios.CancelToken.source()
//updates state to load
dispatch({ type: ACTIONS.MAKE_REQUEST})
axios.get(BASE_URL, {
cancelToken: cancelToken.token,
//pass through params
//hold all other params in spread
params: {
what: 'Data Analyst',
where: 'Melbourne',
...params}
}).then(res => {
//using GET request, dispatch
dispatch({ type: ACTIONS.GET_DATA, payload: {jobs: res.data}})
}).catch(e=>{
if (axios.isCancel(e)) return
dispatch({ type: ACTIONS.ERROR, payload: {error: e} })
})
return () => {
cancelToken.cancel()
}
}, [params, page])
//type is set of data, payload recieved
return state
}
However, despite this I can only access a specific key if i use [1].但是,尽管如此,如果我使用 [1],我只能访问特定的密钥。 For example if I did: return { ...state, loading: false, jobs: action.payload.jobs.results[1].title}
例如,如果我这样做了: return { ...state, loading: false, jobs: action.payload.jobs.results[1].title}
It does work, however I do not know how to map over this in my app to display them on the front end.它确实有效,但是我不知道如何在我的应用程序中映射它以在前端显示它们。
So how would you access each piece and map it over?那么,您将如何访问每个部分并对其进行映射呢? Eg I assume I make a component and pass props, eg something like title, id, description, but I do not know how to access this.
例如,我假设我制作了一个组件并传递道具,例如标题、id、描述之类的东西,但我不知道如何访问它。
Thanks谢谢
mapping in React (I do not fully understand what you asked. I made example with assuming this is what you wanted to know. You can search more about React Mapping here ) React 中的映射(我不完全理解您的要求。我假设这是您想知道的。您可以在此处搜索有关 React 映射的更多信息)
const exampleArray = [{name: "John", age: "23" },{name: "Doe", age: "32"}, ... and more]
const YourComponent = (props)=> <div>
<div>{props.item.name}</div>
<div>{props.item.age}</div>
</div>
const MainComponent = ()=> {
return (
<div>
// Never Forget your key when mapping
{exampleArray.map((item,index)=> <YourComponent key={index} item={item} />)}
</div>
)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.