如何通过数组过滤js中的用户输入?

时间:2018-11-20 07:27:47

标签: javascript arrays filter

我正在尝试构建一个随机报价机,用户可以在其中输入主题词,算法在报价列表中搜索该单词,创建包含该单词的新报价列表,然后随机返回过滤列表中的引号。如果列表中没有单词,它将返回任何随机引用。我不知道如何在JavaScript中创建过滤列表。到目前为止,我已经:

<body>
<p id="quoteDisplay"></p>
</body>
<form>
<input type="text" id="topic" />
<button onclick="wiseQuote()">Quote</button>
</form>

<script type="text/javascript">
var quotes = [
"You've gotta dance like there's nobody watching, Love like you'll never be hurt, Sing like there's nobody listening, And live like it's heaven on earth. - William W. Purkey",
"You know you're in love when you can't fall asleep because reality is finally better than your dreams. - Elbert Hubbard",
"Being deeply loved by someone gives you strength, while loving someone deeply gives you courage - Lao Tzu",
"In three words I can sum up everything I've learned about life: it goes on. - Robert Frost",
"Everything in moderation, including moderation - Oscar Wilde",
"Life isn't about finding yourself. Life is about creating yourself. - George Bernard Shaw",
"Life is like riding a bicycle. To keep your balance, you must keep moving. - Albert Einstein",
"The truth is rarely pure and never simple. - Oscar Wilde",
"A lie can travel half way around the world while the truth is putting on its shoes.- Mark Twain",
"Perhaps one did not want to be loved so much as to be understood. - George Orwell",
"Facts do not cease to exist because they are ignored. - Aldous Huxley",
"Above all, don't lie to yourself. The man who lies to himself and listens to his own lie comes to a point that he cannot distinguish the truth within him, or around him, and so loses all respect for himself and for others. And having no respect he ceases to love. - Fyodor Dostoevsky",
"A thing is not necessarily true because a man dies for it. - Oscar Wilde",
"The unexamined life is not worth living. - Socrates"
]

if (topic != null){
filteredQuotes = quotes.filter(function(){
return ;
});
}

if (filteredQuotes.length) {
randomIndex = Math.floor(Math.random() * filteredQuotes.length);
filteredQuote = filteredQuotes[randomIndex];
} else {
randomIndex = Math.floor(Math.random() * quotes.length);
filteredQuote = quotes[randomIndex];
}

document.getElementById('quoteDisplay').innerHTML = filteredQuote;
}

</script>

这是我正在构建的第一个项目,所以我希望这段代码不会太混乱!!提前非常感谢大家:)

2 个答案:

答案 0 :(得分:0)

您需要在输入和.filter()的功能中获取价值,请检查项目是否具有目标文本。使用.indexOf()进行检查。然后在0和已过滤数组的长度之间生成随机数,并按索引从数组中选择一项。

function wiseQuote(){
  // get value of input
  var topic = document.getElementById("topic").value;
  // filter array based of input value
  var filteredQuotes = quotes.filter(function(val) {
    return val.indexOf(topic) > -1;
  });
  // replace filtered array with origin array if it is empty
  filteredQuotes = filteredQuotes.length > 0 ? filteredQuotes : quotes;
  // generate random number
  var rand = Math.floor(Math.random()*(filteredQuotes.length));
  // insert target item into html
  document.getElementById('quoteDisplay').innerHTML = filteredQuotes[rand];  
}

function wiseQuote(){
  var topic = document.getElementById("topic").value;
  var filteredQuotes = quotes.filter(function(val) {
    return val.indexOf(topic) > -1;
  });
  filteredQuotes = filteredQuotes.length > 0 ? filteredQuotes : quotes;
  var rand = Math.floor(Math.random()*(filteredQuotes.length));
  document.getElementById('quoteDisplay').innerHTML = filteredQuotes[rand];  
}

var quotes = [
  "You've gotta dance like there's nobody watching, Love like you'll never be hurt, Sing like there's nobody listening, And live like it's heaven on earth. - William W. Purkey",
  "You know you're in love when you can't fall asleep because reality is finally better than your dreams. - Elbert Hubbard",
  "Being deeply loved by someone gives you strength, while loving someone deeply gives you courage - Lao Tzu",
  "In three words I can sum up everything I've learned about life: it goes on. - Robert Frost",
  "Everything in moderation, including moderation - Oscar Wilde",
  "Life isn't about finding yourself. Life is about creating yourself. - George Bernard Shaw",
  "Life is like riding a bicycle. To keep your balance, you must keep moving. - Albert Einstein",
  "The truth is rarely pure and never simple. - Oscar Wilde",
  "A lie can travel half way around the world while the truth is putting on its shoes.- Mark Twain",
  "Perhaps one did not want to be loved so much as to be understood. - George Orwell",
  "Facts do not cease to exist because they are ignored. - Aldous Huxley",
  "Above all, don't lie to yourself. The man who lies to himself and listens to his own lie comes to a point that he cannot distinguish the truth within him, or around him, and so loses all respect for himself and for others. And having no respect he ceases to love. - Fyodor Dostoevsky",
  "A thing is not necessarily true because a man dies for it. - Oscar Wilde",
  "The unexamined life is not worth living. - Socrates"
];
<p id="quoteDisplay"></p>
<form>
  <input type="text" id="topic" />
  <button type="button" onclick="wiseQuote()">Quote</button>
</form>

答案 1 :(得分:0)

下面是一个可行的解决方案;但是我想用您的原始代码解决一些问题,以帮助您。

  • 您来自Java背景,或者从某个地方复制了Java解决方案。 JavaScript没有 select * from TRX_T TT join TRX_SUB TS on TT.CODE=TS.CODE and TT.SUBID= TS.ID where TS.VALUE=1 and TS.CODE=1 AND TS.ID=17 and not exists ( select 1 from TRX_T t1 where t1.CODE=TT.code and (t1.STATUS ='T' OR t1.STATUS='R' or t1.STATUS='C') ) ,它是size()。另外,您不能使用length来强制输入类型,因为Javascript是一种松散类型的语言。
  • int<p>内部,但您的<body><form>外部。您需要确保并将用户希望看到的所有内容保留在<body>元素内。
  • <body>希望您返回Array.prototype.filter()true,其他方法均无效。
  • 您的false函数实际上在任何代码启动之前就已关闭,制表符可以帮助您发现这样的错误。
  • 我删除了wiseQuote元素,因为您实际上并未提交。这将使您不必尝试使用<form>来停止刷新页面。

e.preventDefault()
function wiseQuote(){
  var topic = document.getElementById("topic").value;
  var randomIndex;
  var filteredQuotes;
  var filteredQuote;
  
  if (topic != null){
    filteredQuotes = quotes.filter(function(quote){
      return quote.includes(topic);
    });
  }
  
  if (filteredQuotes.length) {
    randomIndex = Math.floor(Math.random() * filteredQuotes.length);
    filteredQuote = filteredQuotes[randomIndex];
  } else {
    randomIndex = Math.floor(Math.random() * quotes.length);
    filteredQuote = quotes[randomIndex];
  }
  
  document.getElementById('quoteDisplay').innerHTML = filteredQuote;
}

var quotes = [
    "You've gotta dance like there's nobody watching, Love like you'll never be hurt, Sing like there's nobody listening, And live like it's heaven on earth. - William W. Purkey",
    "You know you're in love when you can't fall asleep because reality is finally better than your dreams. - Elbert Hubbard",
    "Being deeply loved by someone gives you strength, while loving someone deeply gives you courage - Lao Tzu",
    "In three words I can sum up everything I've learned about life: it goes on. - Robert Frost",
    "Everything in moderation, including moderation - Oscar Wilde",
    "Life isn't about finding yourself. Life is about creating yourself. - George Bernard Shaw",
    "Life is like riding a bicycle. To keep your balance, you must keep moving. - Albert Einstein",
    "The truth is rarely pure and never simple. - Oscar Wilde",
    "A lie can travel half way around the world while the truth is putting on its shoes.- Mark Twain",
    "Perhaps one did not want to be loved so much as to be understood. - George Orwell",
    "Facts do not cease to exist because they are ignored. - Aldous Huxley",
    "Above all, don't lie to yourself. The man who lies to himself and listens to his own lie comes to a point that he cannot distinguish the truth within him, or around him, and so loses all respect for himself and for others. And having no respect he ceases to love. - Fyodor Dostoevsky",
    "A thing is not necessarily true because a man dies for it. - Oscar Wilde",
    "The unexamined life is not worth living. - Socrates"
];