string.includes只检查for循环javascript的最后一个值

时间:2017-02-01 00:38:04

标签: javascript arrays object for-in-loop

我正在制作一个网页,根据“情绪关键字”显示动机报价。

我有一个带数组的对象:

for (let key in moodList) {
  const moodFeeling = key;
  if (askBar.value.includes(moodFeeling)) {
    for (let i = 0; i <moodList[moodFeeling].length; i += 1) {
      motivQuoteResult.push(moodList[moodFeeling][i]);
      const randomResult = Math.floor(Math.random() * motivQuoteResult.length);
      printToPage(motivQuoteResult[randomResult]);
    }
  } else {
    printToPage('<h3>Try again, I don\'t have that feeling on file</h3>');
  }
}

motivQuoteResult = [];

askBar.value = '';
}
});

这是我正在编写的代码,用于检查带有数组的对象的用户输入。当匹配时,它会显示该特定阵列的随机激励报价。但是,如果我写一个else语句,为什么只考虑对象中的最后一个数组?

{{1}}

4 个答案:

答案 0 :(得分:0)

我假设您正在检查用户输入并且用户输入类似“悲伤”的内容。 然后尝试以下代码:

let moodQuotes = moodList[askBar.value];
if(Array.isArray(moodQuotes) && moodQuotes.length) {
    printToPage(moodQuotes[Math.floor(Math.random()*moodQuotes.length)])
}
else {
    ... no quote found do something else
}

如果askbar.value应包含任何可用的情绪,请使用:

let possibleMoods = Object.keys(moodList).filter(mood => askbar.value.indexOf(mood) > -1);
let moodQuotes = possibleMoods.map(mood => moodList[mood]);
if(moodQuotes.length === 0) {
  ... no mood found do something else
}
let possibleQuotes = moodQuotes[Math.random()*possibleQuotes.length];
if(possibleQuotes.length === 0) {
  ... no quotes found for mood, do something else
}
printToPage(possibleQuotes[Math.floor(Math.random()*possibleQuotes.length)])

答案 1 :(得分:0)

我认为你想在循环中做其他事情,如果不是,你甚至不需要循环。而且我不知道你想对motivQuoteResult做些什么。我离开了。但实际上你并不需要将其他阵列的每个元素推送到新阵列。尝试此代码并修改为您的目的:

var askBarValue = askBar.value;
    var hasMoodOnFile = false;
  for (let key in moodList) {
          const moodFeeling = key;

          var    motivQuoteResult = [];
          if (askBar.value.includes(moodFeeling)) {
                  motivQuoteResult = moodList[moodFeeling];
              var randomResult = Math.floor(Math.random() * motivQuoteResult.length);
              result.innerHTML = motivQuoteResult[randomResult];// call your printToPage function instead
              hasMoodOnFile = true;
          } 
  }
  if(!hasMoodOnFile){
          result.innerHTML = '<h3>Try again, I don\'t have that feeling on file</h3>'; // call your printToPage function instead
  }

这是源代码: https://jsfiddle.net/85dxus4z/

答案 2 :(得分:0)

通过逻辑结构的方式,您将调用&#34; printToPage&#34;循环的每次迭代(在&#34; if&#34;分支或&#34; else&#34;分支)。如果printToPage正在覆盖DOM元素的内容,那么您将只看到最后一次迭代的效果。有很多方法可以解决这个问题,但有一种方法是在循环之前将布尔变量(例如foundMatch)初始化为false,并在&#34;中设置为true;如果&#34;声明;摆脱其他;循环之后:     if(!foundMatch){打印你的&#34;再试一次&#34;消息}

答案 3 :(得分:0)

要进行搜索,您必须使用indexOf查看小写toLowerCase中是否包含小写(使用keytext

尝试在下面的示例中键入I'm so sad right now!之类的内容:

&#13;
&#13;
const moodList = {
  sad: [
    '"Dream as if you\'ll live forever, live as if you\'ll die today." James Dean',
    '"Life is a journey, and if you fall in love with the journey, you will be in love forever." Peter Hagerty',
    '"I\'ve learned that people will forget what you said, people will forget what you did, but people will never forget how you made them feel." Maya Angelou'
  ],
  angry: [
    '"For every minute you remain angry, you give up sixty seconds of peace of mind."Ralph Waldo Emerson',
    '"Speak when you are angry - and you\'ll make the best speech you\'ll ever regret." Laurence J. Peter',
    '"Anger and intolerance are the enemies of correct understanding."Mahatma Gandhi'
  ]
};


var askBar = prompt("Ask bar:"); // for the sake of this example I'm using prompt

var printToPage = alert; // for the sake of this example I'm using alert instead of your printToPage

// to see if we found something or not
var found = false;

for (let key in moodList) {
  // change askBar to askBar.value
  if (askBar.toLowerCase().indexOf(key.toLowerCase()) != -1) {
    var randomIndex = Math.floor(Math.random() * moodList[key].length);
    printToPage(moodList[key][randomIndex]);
    found = true; // we found something
    break; // don't search anymore so terminate the loop
  }
}

// now if we are outside the loop and we've found nothing
if (!found)
  printToPage('Try again! I don\'t have that feeling on file!');
&#13;
&#13;
&#13;