Alright so this has been ruining my day, I'm trying to set the property of a value of an object that I've passed into a function but it's just not behaving at all
findFoodItemsWithRelevantNutrients = relevantNutrients => {
let foodItems = [];
let parent = this;
console.log(relevantNutrients);//STACK OVERFLOW: #3
for(let q=0; q<19; q++) {
let food = FoodItems[q];
//Goes through 20 foods present in FoodItems array
let foodProps = Object.entries(food); //breaks each field for a given food object into key/value pairs
food.relevanceScore = 0;
food.keyNutrients = [];
relevantNutrients.forEach(function (nutrientReport) {
//Go through each relevant nutrient passed into this function
//var nutrientReport = relevantNutrients[i];
let currentNutrient = nutrientReport.nutrient;
for(let j=0; j<foodProps.length; j++) {
let prop = foodProps[j];
let propName = prop[0];
let propValue = prop[1];
//see if the nutrient is present in a significant amount in the food
if( propName === currentNutrient.name && propValue > 10 ) {
nutrientReport.RDA = propValue; //set to propValue
console.log(nutrientReport.RDA); //STACK OVERFLOW #1 -shows propValue
console.log(nutrientReport); //STACK OVERFLOW #2 - shows arbitrary value
food.relevanceScore += 10;
food.relevanceScore += nutrientReport.benefits.length + nutrientReport.toxicity.length + nutrientReport.deficiencyArr.length;
food.keyNutrients.push(nutrientReport);
}
}
});
if(food.relevanceScore > 10) {
food = parent.getItemTotalOrganBenefitScores(food);
foodItems.push(food);
}
}
console.log(foodItems);
foodItems.sort(function (foodA, foodB) {
return foodB.relevanceScore - foodA.relevanceScore;
});
return foodItems;
};
My confusions arising from the lines I've prefixed with "STACK OVERFLOW"
- I've set the value of "nutrientReport.RDA" and when I log it, it shows the value that I set it to as expected.
2
problem: when I log the whole object I can't see the value i've just set "nutrientReport.RDA" to. question: why is the value that I set it to not sticking properly?
3
problem: when I log the value of the object passed in as a parameter here, they reflect the values logged in issue #2
question: how is this possible if I've logged it before any values are being set.
extra detail:
this function is within a react component
and relies on the object returned from another function.
like so
this.findFoodItemsWithRelevantNutrients(this.searchNutrientBenefits(this.props.queryVariable))