so i am fetching data from an API on page load in reactJS
useEffect(() => {
fetch('https://xxxxx/getdata',{
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body:JSON.stringify({
Action : "xxxx",
OrganisationID : "xxx-1000",
})
})
.then((res) => res.json())
.then((json) => {
setResponse(json)
console.log(json)
})
.catch((err) => {
console.log(err.message);
});
}, []);
The json returns me an array of 2 objects.
{
"OrganisationJobPosts": [
{
"Languages": [
"English",
"Spanish"
],
"Project_Type": "One-Time Project",
"Description": "This job requires Java and Javascript developers",
"Status": "open",
"Proposal_Questions": [
"question1?",
"question2?"
],
"Entity": "JOBPOST",
"Organization_Name": "ABC Company",
"Jobpost_ID": "JOBPOST-20220608-10",
"Jobpost_Title": "Web Developer",
"Organization_ID": "ORGANIZATION-1000",
"Rates": "$20/hour",
"Experience": "< 3 Years",
"Created_Date": 20220608,
"Location": "Singapore",
"SK": "JOBPOST-20220608-10",
"PK": "ORGANIZATION-1000",
"Skill_Set": [
"HTML",
"Javascript"
]
},
{
"Languages": [
"English",
"French"
],
"Project_Type": "Ongoing Project",
"Description": "This job requires DB design skills",
"Status": "Closed",
"Proposal_Questions": [
"Question A?",
"Question B?"
],
"Entity": "JOBPOST",
"Organization_Name": "ABC Company",
"Jobpost_ID": "JOBPOST-20220608-11",
"Jobpost_Title": "Database Developer",
"Organization_ID": "ORGANIZATION-1000",
"Rates": "$20/hour",
"Experience": "< 3 Years",
"test": [
"test"
],
"Created_Date": 20220610,
"Location": "TBD",
"SK": "JOBPOST-20220608-11",
"PK": "ORGANIZATION-1000",
"Skill_Set": [
"DynaomDB",
"GraphDB"
]
}
]
}
I then tried looping through the said array
<header className="headerTabContent">
{(() => {
for (var key in response) {
if (response.hasOwnProperty(key)) {
const resultarray = response[key]
console.log(resultarray)
for (var i=0;i<=resultarray.length;i++) {
return(<h1 className="heading" onClick={()=>navigate('/dashboard/hire/discover/job-post')}>{resultarray[i].Jobpost_ID}</h1>)
}
}
}
})()};
</header>
As seen my array length is 2. But the loop always outputs only the first array object (Job ID JOBPOST-20220608-10) wheras the second array item (JOBPOST-20220608-11) is not output. I can confirm the size is correct because before the second for loop, I did a console log of the length and found 2 objects. Am I doing something wrong here ?
