简体   繁体   English

Javascript object 中的对象列表包含最后一个列表中的值

[英]List of objects in Javascript object contains the values from the last list

I am facing a problem that the values in the list of objects in some object is containing values only from the last object.我面临一个问题,即某些 object 中的对象列表中的值仅包含来自最后一个 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 score s in the criteriaScore list.这是生成列表的 output 的样子,其中totalScore应该是criteriaScore列表中所有score的总和。 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请注意, Location 1Location 2criteriaScore score中的所有分数值现在都基于Location 3score值(其中也包含正确的totalScore 。请参阅此 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.这是产生上述 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();

You have 3 criteria in criteriaList and you keep overwriting the scores in those criteria items for each of the 3 locations.您在标准列表中有 3 个标准,并且您不断覆盖 3 个位置中每个位置的这些标准项目中的分数。 Your criteriaScore array holds references to these 3 (and only 3) criteria so you'll see the same, final set of 3 scores.您的 criteriaScore 数组包含对这 3 个(并且只有 3 个)标准的引用,因此您将看到相同的最终 3 个分数集。

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).不清楚的一件事是,当成分分数为 31、29、49(总计 109)时,为什么示例中的最终总分值为 30。 I'm assuming that was not actually the real output.我假设这实际上不是真正的 output。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM