I am new to React and I try to get a summation using for loop in react. To do that I use this code.
function CompanyForms() {
const { postId, companyId } = useParams();
const [offers, setOffers] = useState([]);
useEffect(()=>{
getAllOffers();
}, []);
const getAllOffers = async () => {
await axios.get(`/viewPendingCompanyOffers`)
.then ((response)=>{
const allNotes=response.data.existingOffers;
setOffers(allNotes);
})
.catch(error=>console.error(`Error: ${error}`));
}
console.log(offers);
const wasteItem = offers?.filter(wasteItem => wasteItem.status==='accepted' && wasteItem.companyId===companyId);
console.log(wasteItem);
const wasteItemLength = wasteItem.length;
console.log(wasteItemLength);
let quantity=0;
for (let i = 0; i < wasteItemLength; i++) {
quantity += wasteItem.map(wItem=>wItem.quantity)
}
console.log(quantity);
}
First I call the API and get a length 9 array of objects. Then I use the filter function to filter the data that are in length 9 array of objects. As the result of this filter function, I get length two array of objects. This image shows that result.
Then I assign the length of the array of objects that get from the filter function to a variable const wasteItemLength = wasteItem.length. Then I use a for loop with operators to get the summation of quantity in two array of objects called as wasteItem.
The quantity field has a value of 20 in both wasteItem array of objects. I try to add these two values using the for a loop. Then when using this console.log(quantity), I should get 40 as the answer. But the problem is I get this
as the result instead of value 40. What is the reason for this problem? How do I solve this problem?
Thank you.

