Home > Blockchain >  List of objects in Javascript object contains the values from the last list
List of objects in Javascript object contains the values from the last list

Time:11-19

I am facing a problem that the values in the list of objects in some object is containing values only from the last object.

What I am doing here is that I want to assign score to the locations based on different critera (therefore, I also have a list of criteria too). For simplicty, I am assigning a random number to criteria score.

Here is what the output of generated list looks like where the totalScore should be the sum of all scores in the criteriaScore list. Please observe that all values of score in the criteriaScore of Location 1, Location 2 are now based on score values of Location 3 (which also contains the correct totalScore. Please see the code below this output

[
  {
    name: 'Location 1',
    score: {
      totalScore: 113,
      criteriaScore: [
        {
          description: 'Desc. of Criteria 1 ....',
          score: 31
        },
        {
          description: 'Desc. of Criteria 2 ...',
          score: 29
        },
        {
          description: 'Desc. of Criteria 3 ...',
          score: 49
        }
      ]
    }
  },
  {
    name: 'Location 2',
    score: {
      totalScore: 52,
      criteriaScore: [
        {
          description: 'Desc. of Criteria 1 ....',
          score: 31
        },
        {
          description: 'Desc. of Criteria 2 ...',
          score: 29
        },
        {
          description: 'Desc. of Criteria 3 ...',
          score: 49
        }
      ]
    }
  },
  {
    name: 'Location 3',
    score: {
      totalScore: 30,
      criteriaScore: [
        {
          description: 'Desc. of Criteria 1 ....',
          score: 31
        },
        {
          description: 'Desc. of Criteria 2 ...',
          score: 29
        },
        {
          description: 'Desc. of Criteria 3 ...',
          score: 49
        }
      ]
    }
  }
]

And here is the code which is producing the above output. Can someone explain what I am doing wrong here? Thanks in advance.

// criteria list on which the assessment of locations will be performed
// this initially contains only description
let criteriaList = [
  {
    description: 'Desc. of Criteria 1 ....'
  },
  {
    description: 'Desc. of Criteria 2 ...'
  },
  {
    description: 'Desc. of Criteria 3 ...'
  }
];

// names of locations which will be scored based on above criteria list
let locationList = [
  {
    name: 'Location 1'
  },
  {
    name: 'Location 2'
  },
  {
    name: 'Location 3'
  }
];

const calcAllLocationsScore = () => {
  let locationScoreList = [];
  locationList.forEach(location => {
      locationScoreList.push({
        ...location,
        score: calcLocScore()  
      });
  });
  
  return locationScoreList;  
};

const calcLocScore = () => {
  let locScore = {
    totalScore: 0,
    criteriaScore: [] // each time the criteriaScore is initialized to empty list 
  }
  let index = 0;
  criteriaList.forEach(criteria => {
    criteria.score =  Math.floor((Math.random() * 50)   0); // add score key and just assign the random score for simplicty to make sure different number is assigned for each critera
    locScore.totalScore  = criteria.score;
    locScore.criteriaScore.push(criteria); // add the criteria score object to criteriaScore list
  });
    
  return locScore;
};
// and finally when this function is called, the list of locations with the scores is produced containing actual ```totalScore``` but ```criteriaScore``` of all locations has value from the last location only 
calcAllLocationsScore();

CodePudding user response:

You have 3 criteria in criteriaList and you keep overwriting the scores in those criteria items for each of the 3 locations. Your criteriaScore array holds references to these 3 (and only 3) criteria so you'll see the same, final set of 3 scores.

Change your criteria list loop as follows. to create new criteria each time:

criteriaList.forEach(criteria => {
    const newcriteria = {...criteria};
    newcriteria.score = Math.floor((Math.random() * 50)   0);
    locScore.totalScore  = newcriteria.score;
    locScore.criteriaScore.push(newcriteria);
});

One thing that's not clear is why the final totalScore value in your example is 30 when the constituent scores are 31,29,49 (totalling 109). I'm assuming that was not actually the real output.

  • Related