简体   繁体   中英

Javascript join() array

This might be very simple but I am trying to use the join() array in order to remove the - from the last item on the days array.

How can I do that? This is my code:

var days = [
  'Monday',
  'Tuesday',
  'Wednesday',
  'Thursday',
  'Friday',
  'Saturday',
  'Sunday'
];

var counter = 0;
while (counter < days.length) {
  document.write(days[counter]);
  counter++;
  days.join(' - ');
}

You don't need a loop. It's very simple:

  var days = [ 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday' ]; document.write(days.join(", ")); 

I don't recommend the use of document.write. It's dangerous. Use DOM methods instead:

document.getElementById("layer").innerHTML = days.join(", ");

And the HTML as simple as this:

<div id="layer"></div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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