[英]how to add spaces between array items javascript
I am a beginner at javascript so I appreciate any help/advice given.我是 javascript 的初学者,所以我很感激任何帮助/建议。
My issue here is that I'm trying to figure out how to add space between items in the times[] array which holds the values for showtimes for each movie object.我的问题是我试图弄清楚如何在 times[] 数组中的项目之间添加空格,该数组保存每部电影 object 的放映时间值。 I tried to split at the comma (",") but that isn't working.
我试图用逗号(“,”)分割,但这不起作用。 When I tried splitting at ("pm") that worked.
当我尝试在(“pm”)进行拆分时,它起作用了。 I also figured out a way to add space in the showtimes values themselves, but I thought that there must be a better way to go about it.
我还想出了一种在放映时间值本身中添加空间的方法,但我认为必须有更好的方法来 go 关于它。 Any thoughts?
有什么想法吗? thanks!
谢谢!
window.onload = init;
function Movie(title, year, showtimes) {
this.title = title;
this.year = year;
this.showtimes = showtimes;
}
function init() {
var darkKnight = new Movie("The Dark Knight Rises", "2012", ["1pm,", "2pm,", "3pm,"]);
var takeThisWaltz = new Movie ("Take This Waltz", "2012", ["4pm", "5pm","6pm"]);
var watchmen = new Movie("Watchmen", "2009", ["7pm","8pm","9pm"]);
var movieList = [darkKnight, takeThisWaltz, watchmen]
for(var i = 0; i < movieList.length; i++) {
var theObj = movieList[i];
var times = [movieList[i].showtimes]
for(var j = 0; j < times.length; j++) {
var str = times[i];
}
makeResult(); }
function makeResult() {
var list = document.getElementById("movieList");
var li = document.createElement("li");
li.innerHTML = "title: " + movieList[i].title + " year: " + movieList[i].year + " showtimes: " + times[0].toString().split(",");
list.appendChild(li); }
}
the array.join
function takes an optional delimiter
parameter which will seperate the elements by the delimiter. array.join
函数采用可选的delimiter
参数,该参数将通过分隔符分隔元素。 A simple example: 一个简单的例子:
var showtimes = ["1pm", "2pm", "3pm"];
var showtimesAsString = showtimes.join(', '); // gives "1pm, 2pm, 3pm"
const names = ["alice", "bob", "charlie", "danielle"] const capitalized = names.map((name) => { return name[0].toUpperCase() + name.slice(1) }) console.log(capitalized)
["Alice", "Bob", "Charlie", "Danielle"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.