简体   繁体   English

我不想在javascript上重复相同的代码**新**

[英]I don't want to repeat the same code on javascript **new **

I'm quite new at this.... its to create a quote from three sections of the phrase: start. 我对此很陌生。...从短语的三个部分创建报价:开始。 middle and end... and randomly select one from each. 中间和结尾...,然后从中随机选择一个。

The code works but I didn't want to repeat the code underneath with the Math.floor/Math.Random three times. 该代码有效,但是我不想在Math.floor / Math.Random下重复三遍代码。

Any ideas on how to not repeat the code? 关于如何不重复代码的任何想法?

Thanks! 谢谢!

var quotes = {
  start: ["start1 ", "start2 ", "start3 "],
  middle: ["middle1 ", "middle2 ", "middle3 "],
  end: ["end1 ", "end2 ", "end3 "],
 };
 var quoteRandom = "";
 for (var y in quotes) {
 console.log(quotes[y]);
}

 quoteRandom += quotes.start[Math.floor(Math.random() * 3)]; 
 quoteRandom += " " + quotes.middle[Math.floor(Math.random() * 3)]; 
 quoteRandom += " " + quotes.end[Math.floor(Math.random() * 3)];

 console.log(quoteRandom);

You could take the power of a function and a callback, which is basically a function for another function which calls the function for each of item of an array, for example. 您可以利用一个函数和一个回调的功能,例如,它基本上是另一个函数的函数,该另一个函数为数组的每个项目调用该函数。

Methods used: 使用的方法:

  • Object.values for getting all values of an object Object.values用于获取对象的所有值

  • Array#map for returning a value for each item (in this case take the array and return a random item) Array#map用于为每个项目返回一个值(在这种情况下,请使用该数组并返回一个随机项目)

  • and a function for getting a random value out of an array. 以及从数组中获取随机值的函数。

 function getRandomValue(array) { return array[Math.floor(Math.random() * array.length)]; } var quotes = { start: ["start1 ", "start2 ", "start3 "], middle: ["middle1 ", "middle2 ", "middle3 "], end: ["end1 ", "end2 ", "end3 "], }, quoteRandom = Object.values(quotes).map(getRandomValue).join(' '); console.log(quoteRandom); 

Just wrap Math.floor(Math.random() * 3) as return value of function and reuse same function where ever required to avoid duplicate lines of code 只需将Math.floor(Math.random()* 3)包装为函数的返回值,并在需要避免重复代码行的地方重用相同的函数

 var quotes = { start: ["start1 ", "start2 ", "start3 "], middle: ["middle1 ", "middle2 ", "middle3 "], end: ["end1 ", "end2 ", "end3 "], }; var quoteRandom = ""; for (var y in quotes) { console.log(quotes[y]); } function test(){ return Math.floor(Math.random() * 3) } quoteRandom += quotes.start[test()]; quoteRandom += " " + quotes.middle[test()]; quoteRandom += " " + quotes.end[test()]; console.log(quoteRandom); 

code sample - https://codepen.io/nagasai/pen/zWGwPg?editors=1111 代码示例-https: //codepen.io/nagasai/pen/zWGwPg?editors=1111

use array.reduce() . 使用array.reduce() This way you don't have to worry about start , middle and end length 这样,您不必担心startmiddleend长度

 var quotes = { start: ["start1 ", "start2 ", "start3 "], middle: ["middle1 ", "middle2 ", "middle3 "], end: ["end1 ", "end2 ", "end3 ","end4 ", "end5 ", "end6"], }; var quoteRandom = [quotes.start,quotes.middle,quotes.end].reduce((str,el)=> {return str+=el[Math.floor(Math.random() * el.length)]},""); console.log(quoteRandom); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM