【问题标题】:Saving object property to a variable将对象属性保存到变量
【发布时间】:2017-09-26 09:51:16
【问题描述】:

我对对象(实际上是它们的属性)和变量有疑问。 情况如下:

var mainLANG = ???;

if(words[i].pol == wordToCheck){
      if(words[i].eng == wordTyped){
        correct = true;
        update_counter('.goodWords', '+', 1);
        update_counter('.allWords', '-', 1);
        elementToRemove = i;
      }else {
        update_counter('.badWords', '+', 1);
      }

现在,我静态插入了 pol 和 eng 等属性(此属性将发生变化)[ pol 到 eng 和 eng 到 pol - 由某些事件触发]。我有一个想法来声明一个可以存储有关 lang 信息的变量(例如,现在 mainLANG 是 pol - 在事件之后是例如 eng)。我应该在 mainLANG 中输入什么以及如何替换单词“pol”。我希望用 mainLANG 的值替换的单词 'pol' 被引用到 mainLANG。

也许不那么复杂(示例):

var mainLANG = pol;
if(words[i].mainLANG .... - Error... unexpected property

var mainLANG = 'pol'
if(words[i].mainLANG .... - Another error, don't even remember its content.

你有什么想法吗?

【问题讨论】:

  • 请同时添加所有缺失的变量。
  • mainLANG 不是 words[i] 的属性,而是一个变量
  • @marvel308:我知道 mainLANG 是可变的,严格来说我希望 words[i] 的属性是动态的 :)(一次 - pol,另一次 eng)

标签: javascript jquery variables object javascript-objects


【解决方案1】:

我已经做了一个设置,我认为它可以满足您的要求。

let
  // Variable to keep track of the current language.
  currentLang;
  
const
  // Mapping for a language to its code.
  languages = {
    enligh: 'eng',
    polish: 'pol'
  },
  // Scores object.
  scores = {
    goodWords: 0,
    badWords: 0,
    allWords: 2
  },
  // The words with their translation per language.
  words = [
    {
      [languages.polish]: 'A',
      [languages.english]: 'Aa'
    },
    {
      [languages.polish]: 'B',
      [languages.english]: 'Bb'
    },
    { 
      [languages.polish]: 'C',
      [languages.english]: 'Cc'
    }
  ];


/**
 * Checks if a word is valid in the current language.
 *
 * @param {Object} translations  An object with properties per language code with the right translation.
 * @param {String} word          The word as typed by the user, needs to be checked against the proper translation.
 *
 * @returns {Boolean} The method returns true when the typed word matches the translation for the current language; otherwise false.
 */
function checkWord(translations, word) {
  // Check if the word matches the translation for the current language.
  if (translations[currentLang] === word) {
    // Update the score counters.
    scores.goodWords++;
    scores.allWords--;
    
    // Return true, the word matches.
    return true;
  }
  
  // Update the score counters.
  scores.badWords++;
  // Return false, the word doesn't match the translation.
  return false;
}


// Set the current language to Polish.
currentLang = languages.polish;
console.log(`Is "A" a valid Polish word: ${checkWord(words[0], 'A')}`);
console.log(`Is "Aa" a valid Polish word: ${checkWord(words[0], 'Aa')}`);

// Set the current language to English.
currentLang = languages.english;
console.log(`Is "A" a valid English word: ${checkWord(words[0], 'A')}`);
console.log(`Is "Aa" a valid English word: ${checkWord(words[0], 'Aa')}`);

【讨论】:

  • 这就是我要找的。非常感谢:)
  • 太好了,很高兴能帮到您!
  • 如果它回答了您的问题,请不要忘记接受答案。
猜你喜欢
  • 2017-07-10
  • 1970-01-01
  • 1970-01-01
  • 2023-02-12
  • 1970-01-01
  • 2020-04-29
  • 2017-08-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多