I am new in react native , I want to insert new value into every object of array.
I have successfully implemented the logic according to object value. Condition is working right, I have checked by console.log() the statement working right but when I print the my final output array the new value of every object is same, value not assign different in object according to condition.
Here is the code which I am using:-
var newOffers = [];
response.data.forEach((element) => {
for (let i = 0; i < element.frequency; i++) {
var offer = element;
if (offer.used == 0) {
offer.isAvailable = "true";
newOffers.push(offer);
} else {
if (offer.frequency - i <= offer.used) {
console.log("True calling", offer.frequency - i);
offer.isAvailable = "false";
newOffers.push(offer);
} else {
console.log("False calling", offer.frequency - i);
offer.isAvailable = "true";
newOffers.push(offer);
}
}
}
});
console.log("All offers ", newOffers);
I have assign "isAvailable" value "true" or "false" to offer object and all condition working perfectly but when I print the "newOffers" array after complete the loop process the all "isAvailable" values is "false"
What wrong with this code? can someone help me?
response.dataand what is the expected output.true, prove to yourself (before you prove to us) that the preconditions for those cases even apply. Because if you only seefalseit stands to reason thatoffer.used === 0is false (don't use==, use===unless you know exactly why you need type coercion) and thatoffer.frequency - i <= offer.usedis false. Also, if you're usinglet, don't also use the legacyvar, stick withletfor mutables andconstfor immutables.