2017-09-26 135 views
-3

我有一個對象(實際上是它們的屬性)和變量的問題。 這裏的情況是:將對象屬性保存到變量

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和工程靜態插入屬性(屬性這將改變)POL到ENG和工程到POL - 某些事件觸發。我有一個想法來聲明一個可以存儲有關lang的信息的變量(例如,現在mainLANG是pol - 事件發生後是例如eng)。我應該如何輸入mainLANG以及如何替換單詞「pol」。我想單詞'pol'被mainLANG的值替換爲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. 

你有什麼想法?

+0

請添加所有缺少的變量也是如此。 –

+0

mainLANG不是單詞[i]的屬性,而是一個變量而不是 – marvel308

+0

也請提供[最小,完整和可驗證的示例](https://stackoverflow.com/help/mcve) – lumio

回答

0

我已經做了一個設置,我認爲做你要求的。

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')}`);

+0

這就是我正在尋找的。非常感謝你:) –

+0

太棒了,很高興能有幫助! – Thijs

+0

如果回答您的問題,請不要忘記接受答案。 – Thijs