Javascript 如何在循环中使用对象的新版本,而不覆盖它?

Javascript 如何在循环中使用对象的新版本,而不覆盖它?,javascript,ecmascript-6,Javascript,Ecmascript 6,我有一个函数,它在一个单词数组上循环,并检查传递给该函数的字符串是否包含单词中的任何字母。返回字母匹配最多的单词 我的问题是,我正在复制到循环作用域的const被覆盖。具体地说,let loopIndex=alphalondex如何生成可以在循环中操作的变量,而不重写常量 import _forEach from 'lodash/foreach'; const dictionary = require('./dictionary.json'); /** * @constructor */

我有一个函数,它在一个单词数组上循环,并检查传递给该函数的字符串是否包含单词中的任何字母。返回字母匹配最多的单词

我的问题是,我正在复制到循环作用域的
const
被覆盖。具体地说,
let loopIndex=alphalondex如何生成可以在循环中操作的变量,而不重写
常量

import _forEach from 'lodash/foreach';
const dictionary = require('./dictionary.json');

/**
 * @constructor
 */
var Game = function () {
};

/**
 * @desc Return an object of the repeating string characters.
 * @param string
 * @returns {{}}
 */
Game.prototype.setAlphabet = function (string) {
  let alphabet = {};
  for (let i = 0; i < string.length; i += 1) {
    if (string[i] in alphabet) {
      alphabet[string[i]] += 1;
    } else {
      alphabet[string[i]] = 1;
    }
  }
  return alphabet;
};

/**
 * @desc Return the largest word using the string passed to the function.
 * @param string
 */
Game.prototype.getWinningWord = function (string) {
  let max      = 0;
  let maxIndex = 0;

  // Get the amount of letters used in the string as an object.
  const alphabetIndex = this.setAlphabet(string);

  // Loop through every word in the dictionary
  _forEach(dictionary, function (word, position) {
    let sum       = 0;
    let loopIndex = alphabetIndex;
    // For each of the letters, check the index; and if the letter exists in the word, add to the sum.
    for (let i = 0; i < word.length; i += 1) {
      if (loopIndex[word[i]] > 0) {
        sum += 1;
        loopIndex[word[i]] -= 1;
      }
    }
    // If the current word has more matching letters than the previous max, set this word to max.
    if (sum > max) {
      max      = sum;
      maxIndex = position;
    }
    if (position > 10) return false;
  });
  return dictionary[maxIndex];
};

/**
 * Init game.
 * @type {Game}
 */
var newGame = new Game();
console.log(newGame.getWinningWord('eeosnndsm'));
从“lodash/forEach”导入forEach;
const dictionary=require('./dictionary.json');
/**
*@constructor
*/
var Game=函数(){
};
/**
*@desc返回一个重复字符串字符的对象。
*@param字符串
*@returns{{}
*/
Game.prototype.setAlphabet=函数(字符串){
让字母表={};
for(设i=0;i0){
总和+=1;
循环索引[word[i]]-=1;
}
}
//如果当前单词的匹配字母数大于上一个最大值,请将此单词设置为最大值。
如果(总和>最大值){
最大值=总和;
maxIndex=位置;
}
如果(位置>10)返回false;
});
返回字典[maxIndex];
};
/**
*初始化游戏。
*@type{Game}
*/
var newGame=新游戏();
console.log(newGame.getWinningWord('eeosnndsm');

这里的区别在于,虽然一个
常量
变量永远不能被再次赋值,但如果该变量的值是一个引用,那么该引用可以更改。为了避免这种情况,您需要在循环的每次迭代中创建一个新副本,一种简单的方法是使用:


这里的区别是,虽然一个
const
变量永远不能被再次赋值,但如果该变量的值是一个引用,那么该引用可以更改。为了避免这种情况,您需要在循环的每次迭代中创建一个新副本,一种简单的方法是使用:


您必须在每次迭代中复制
alphalondex
对象。
const
声明使得无法更改变量
alphalondex
的值,但无法更改它引用的对象的内容。除非我遗漏了什么,否则您只需执行类似myWorkingVersion=yourOldObject的操作。然后对myWorkingVersion(原始对象的副本)执行您需要执行的任何操作。您必须在每次迭代中复制
AlphaoIndex
对象。
const
声明使得无法更改变量
alphalondex
的值,但无法更改它引用的对象的内容。除非我遗漏了什么,否则您只需执行类似myWorkingVersion=yourOldObject的操作。然后用myWorkingVersion(原始对象的副本)做任何你需要做的事情。非常感谢,这让我发疯了!非常感谢,这让我快发疯了!
let loopIndex = Object.assign({}, alphabetIndex);