如何在没有括号javascript的情况下返回数组

时间:2018-03-27 00:35:48

标签: javascript arrays string

所以我有这个代码,我有一个句子数组,通过一个开关提示用户要询问他们想要显示多少句子1 - 5.然后它将循环并随机显示的数量从阵列中请求的句子。



var sentences = ["sentence one", "sentence two", "sentence three",
  "sentence 4", "sentence 5"];

function shuffle(a) {
  for (i = a.length - 1; i; i--) {
    let j = Math.floor(Math.random() * i);
    [a[i - 1], a[j]] = [a[j], a[i - 1]];
  }
}

var random = prompt("how many sentences do you want?");
switch (random) {
  case "1":
    shuffle(sentences)
    console.log(sentences(0, 1));
    break;

  case "2":
    shuffle(sentences)
    console.log(sentences.slice(0, 2))
    break;

  case "3":
    shuffle(sentences)
    console.log(sentences.slice(0, 3))
    break;

  case "4":
    shuffle(sentences)
    console.log(sentences.slice(0, 4))
    break;

  case "5":
    shuffle(sentences)
    console.log(sentences.slice(0, 5))
    break;

  default:
    console.log("incorrect number");
}




代码工作正常,但是当它返回时,它会显示当然的数组: 如果这个人想要3个句子:

(3) ["sentence two", "sentence four", "sentence five"]

我想返回句子本身而不是数组:

sentence two, sentence four, sentence five

我尝试使用.length转换为字符串,但这会返回字符串中的字符数,例如输入的提示为5,因此返回:

sente

任何人都有任何想法,非常感谢:)

5 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

你可以通过"加入"你的数组有join method。例如,给定["foo", "bar"]数组,调用["foo", "bar"].join(", ")将输出包含foo, bar的字符串。

以下是一个例子:

> var foo = ['bar', 'bat'];
[ 'bar', 'bat' ]
> foo.join(', ');
'bar, bat'

答案 2 :(得分:0)

要实现您的目标,您可以在ES6中使用传播...运算符。

var sentences = ["sentence one", "sentence two", "sentence three", 
"sentence 4", "sentence 5"];

  function shuffle(a) {
        for (i = a.length - 1; i; i--) {
            let j = Math.floor(Math.random() * i);
            [a[i - 1], a[j]] = [a[j], a[i - 1]];
        }
    }

 var random = prompt("how many sentences do you want?");
  switch (random) {
    case "1":
    shuffle(sentences)
   console.log(...sentences(0, 1));
      break;

      case "2":
       shuffle(sentences)
      console.log(...sentences.slice(0, 2))
      break;

      case "3":
       shuffle(sentences)
      console.log(...sentences.slice(0, 3))
      break;

      case "4":
       shuffle(sentences)
      console.log(...sentences.slice(0, 4))
      break;

      case "5":
       shuffle(sentences)
      console.log(...sentences.slice(0, 5))
      break;

      default:
      console.log("incorrect number");
}

作为旁注,switch语句是重复且不必要的:

 var random = +prompt("how many sentences do you want?");
 if (0 <= random <= 5) {
   shuffle(sentences);
   console.log(...sentences.slice(0, random));
 } else {
   console.log("invalid number");
 }

这里,+将数字字符串转换为数字。您还可以使用Number(prompt(..))显式转换。

编辑:我看到现在你想在元素之间插入逗号。

您可以使用Array.prototype.join

 var random = +prompt("how many sentences do you want?");
 if (0 <= random <= 5) {
   shuffle(sentences);
   console.log(sentences.slice(0, random).join(', ');
 } else {
   console.log("invalid number");
 }

答案 3 :(得分:0)

使用join()将数组中的所有值与字符串连接起来(如', '

&#13;
&#13;
var sentences = ["sentence one", "sentence two", "sentence three",
  "sentence 4", "sentence 5"];

function shuffle(a) {
  for (i = a.length - 1; i; i--) {
    let j = Math.floor(Math.random() * i);
    [a[i - 1], a[j]] = [a[j], a[i - 1]];
  }
}

var random = prompt("how many sentences do you want?");

shuffle(sentences);

switch (random) {
  case "1":
    sentences = sentences.slice(0, 1);
    break;

  case "2":
    sentences = sentences.slice(0, 2);
    break;

  case "3":
    sentences = sentences.slice(0, 3);
    break;

  case "4":
    sentences = sentences.slice(0, 4)
    break;

  case "5":
    sentences = sentences.slice(0, 5);
    break;

  default:
    console.log("incorrect number");
}

console.log(sentences.join(', '));
&#13;
&#13;
&#13;

答案 4 :(得分:0)

您可以使用join ()。 join()方法将数组的所有元素(或类数组对象)连接成一个字符串并返回该字符串。

在您的代码中:

 console.log((sentences.slice(0, 3)).join(', '));