javascript - 在JS中分解字符串

标签 javascript

我构建了一个脚本,用于从数组中随机抓取报价并显示它。

我正在尝试对其进行格式化,以便将引用和作者分开,如下所示:

“插入引用”
说引用的人的姓名

我尝试使用 split 与 \n<br />即使在警报中,也没有任何效果。

这是我的代码:

//Initalize the array
var quotes = [];

//Insert data into the array
quotes[0] = "It doesn't matter how many times you have failed, you only have             to be right once." + "Mark Cuban";
quotes[1] = "Video games are bad for you? That's what they said about rock n' roll." + "Shigeru Miyamoto";
quotes[2] = "I'd like to be known as the person who saw things from a                           different point of view to others." + "Shigeru Miyamoto";
quotes[3] = "Stay hungry, stay foolish, stay crazy."  + "Steve Jobs";
quotes[4] = "The future was uncertain, absolutely, and there were many hurdles, twists, and turns to come, but as long as I kept moving forward, one foot in front of the other, the voices of fear and shame, the messages from those who wanted me to believe that I wasn't good enough, would be stilled." + "Chris Gardner";
quotes[5] = "Running a start-up is like eating glass. You just start to like the taste of your own blood." + "Sean Parker";
quotes[6] = "I used to drink cristal, the muh'fucker's racist. So I switched gold bottles on to that Spade shit" + "Shawn Carter (Jay Z)";
quotes[7] = "I think it's better to let my work do the talking" + "Shigeru Miyamoto.";
quotes[8] = "Success is a lousy teacher. It seduces smart people into thinking they can't lose." + "Bill Gates";
quotes[9] = "We need to reengineer companies to focus on figuring out who the customer is, what's the market and what kind of product you should build." + "Eric Ries";
quotes[10] = "I have no friends and no enemies - only competitors." + "Aristole Onassis";
quotes[11] = "Working 24 hours a day isn't enough anymore. You have to be willing to sacrifice everything to be successful, including your personal life, your family life, maybe more. If people think it's any less, they're wrong, and they will fail." + "Kevin O'Leary";
quotes[12] = "My hope is to the see the benefits of my labour spread out in the community." + "W. Brett Wilson";
quotes[13] = "I'm not here to make friends; I'm here to make money." + "Kevin O'Leary";
quotes[14] = "Good artists copy, great artists steal" + "Pablo Picasso";
quotes[15] = "Welcome ladies and gentlemen to the eighth wonder of the world. The flow of the century, always timeless; HOV!" + "Shawn Carter (Jay Z)";
quotes[16] = "Today’s “best practices” lead to dead ends; the best paths are new and untried." + "Peter Thiel";
quotes[17] = "I believe life is an intelligent thing: that things aren't random." + "Steve Jobs";
quotes[18] = "Pretty? You mean like rainbows, unicorns, and sparkles?" + "Michelle Brown";
quotes[19] = ".....and for that reason, I'm OUT!" +  "Mark Cuban";

//Splits the quote into two pieces, the quote and the person.

var quoteSplit = function (quotes) {
    var split = quotes.split("+").replace("\n");
}

//Displays a quote from the array at random.

var displayQuote = quotes[Math.floor(20 * Math.random())];
document.write(displayQuote);

//END

最佳答案

当您构建数组时,您正在将引用与作者连接起来。所以这个:

quotes[0] = "It doesn't matter how many times you have failed, you only have to be right once." + "Mark Cuban";

最终将此字符串设置为 quotes[0]

It doesn't matter how many times you have failed, you only have to be right once.Mark Cuban

并且您的 split 语句将不起作用,因为 + 不包含在字符串中。不过,这并不是设置阵列的好方法。例如,如果您的报价包含 + 符号,会发生什么情况?

更好的方法是为每个项目创建一个对象:

quotes[0] = {
  text: "It doesn't matter how many times you have failed, you only have to be right once.",
  author: "Mark Cuban"
}

然后你可以这样做:

var displayQuote = quotes[Math.floor(20 * Math.random())];
document.write(displayQuote.text + '<br>' + displayQuote.author);

关于javascript - 在JS中分解字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32255900/

相关文章:

javascript - 如何从数组中引用 JSX 组件 props?

javascript - 使用 Chai - 如何检查对象属性是否包含 DOM 元素

javascript - 当发生在 child 中的事件时,我需要更改存储在parent 中的状态

javascript - 隐藏 Shield UI 图表的 X 轴

javascript - 过滤绑定(bind)到 knockout.js 模型中的可观察数组的用户 ListView

javascript - 关闭推送内容的 Canvas 边栏?

javascript - 我的 JQuery 脚本没有检索表单文本值

javascript - 验证来自 Mongo 的 JSON?

javascript - 如何让一些代码点表情符号值发挥作用

javascript - 如何使用 node.js 检查文本文件中的重复行?