0

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"

  1. 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))

Community
  • 1
  • 1
  • 1
    I think you _have_ to have parentheses around `relevantNutrients`, like so, `findFoodItemsWithRelevantNutrients = (relevantNutrients) => {...` – Jason Oct 01 '17 at 01:23
  • 1
    There's also a typo in this line `let food = FoodItems[q];`, it should be `let food = foodItems[q];` (notice the lowercase 'f'). Lastly, `findFoodItemsWithRelevantNutrients` is undefined, but I'm guessing it's defined elsewhere and you just didn't include that bit of code but I'm pointing it out just in case. – Jason Oct 01 '17 at 01:31
  • @JasonFry my apologies capital "F" FoodItems is defined earlier out of the scope of this function – Aaron Jackson Oct 01 '17 at 01:42
  • maybe something to do with this https://stackoverflow.com/questions/12482961/is-it-possible-to-change-values-of-the-array-when-doing-foreach-in-javascript – diedu Oct 01 '17 at 03:14
  • and what exactly do you mean with "arbitrary value"? – diedu Oct 01 '17 at 03:19
  • @diedu it assigns the last value in the foodProps array, sometimes the last few values in the foodProps array depending on the object passed to the function – Aaron Jackson Oct 01 '17 at 04:29

0 Answers0