Make a list comma and “and”-separated [closed]

2019-09-26 07:22发布

You have strings with a separator character (a space or something else) like the following:

"1 2 3"
"1 2"
"1"

What is the best/elegant way in Javascript to format them in the following way?

"1, 2 and 3"
"1 and 2"
"1"

Must works for any number of elements >= 1

5条回答
Viruses.
2楼-- · 2019-09-26 07:45

You can use this:

<!DOCTYPE html>
<html>
<body>
<script>
cars=["Elem 1","Elem 2","Elem 3","Elem 4"];
var s = '';
var sep = '';
for (var i=0;i<cars.length;i++)
{
  if(i==cars.length-1){
    s=s+" and "+cars[i];
  }else{
   s=s+sep+cars[i];
   sep = ', ';
  }
}
document.write(s + "<br>");
</script>
</body>
</html>
查看更多
闹够了就滚
3楼-- · 2019-09-26 07:54

Got this somewhere on StackOverflow a few years ago. This is a very basic implemenation -- it wouldn't be hard to add a delimiter parameter to the method.

    function formatList(myString){
        if ( myString.indexOf(" ") === -1 ) return myString;

        if ( myString.indexOf(" ") === myString.lastIndexOf(" ") ){
            return myString.replace(" ", " and ");
        } 

        var former = myString.substr(0, myString.lastIndexOf(" "));
            former = former.replace(/ /g,", ");

        var latter = myString.substr(myString.lastIndexOf(" "), myString.length -1);
        var output = former + " and " + latter;

        return output; 
    }

formatList("1"); // 1
formatList("1 2"); // 1 and 2
formatList("1 2 3"); // 1, 2 and 3
formatList("1 2 3 4"); // 1, 2, 3 and 4
查看更多
淡お忘
4楼-- · 2019-09-26 08:04

A basic solution :

function format(input, separator) {
    if (!input) return input;
    input = input.split(separator || ' ');
    if (input.length === 1) return input[0];
    return input.slice(0, -1).join(', ') + ' and ' + input.pop();
}

This one sticks to your own approach (trailing separator) :

function format(input, separator) {
    var save = input, pattern;
    if (!input) return input;
    pattern = '[^\\' + (separator || ' ') + ']+';
    input = input.match(new RegExp(pattern, 'g'));
    if (!input) return save;
    if (input.length === 1) return input[0];
    return input.slice(0, -1).join(', ') + ' and ' + input.pop();
}

Usage example :

format('1)))2(3)4))', ')'); // "1, 2(3 and 4"
查看更多
时光不老,我们不散
5楼-- · 2019-09-26 08:09

This one works if you have an array too. It uses the Query $.inArray that can be omitted if you do not use jQuery and input only in array form or string form.

  // Transform a string made of substrings separated by a separator (default "#")
  // or an array in a string that is comma (", ") separated and that has an "and" 
  // separaing the last element. 
  // Ex: 
  //     toCommaAnd("1#2#3")     -> "1, 2 and 3"
  //     toCommaAnd("1 2 3"," ") -> "1, 2 and 3"
  //     toCommaAnd("Mr. Cinelli#Ms. Cinelli") ->"Mr. Cinelli and Ms. Cinelli"
  //     toCommaAnd(["a", "b"])  -> "a and b"
  //
  // !!  toCommaAnd("1#2#3#")    ->   become "1, 2 and 3" TOO so you can 
  //                                  concatenate  elements in loops without 
  //                                  caring about special case for first or
  //                                  last element.  


  function toCommaAnd(str, separator) {
      var ar = $.isArray(str) ? str : str.split(separator || "#"),  
          commaUntil = ar.length - 1,
          ret = ar[0], 
          i;

      //This is just for the 1#2#3# cases.
      if (commaUntil > 0 && ar[commaUntil] === ""){
          commaUntil--;
      }

      for (i = 1; i < commaUntil; i++){
          ret += ", " + ar[i];
      }
      if (commaUntil > 0)
          ret += " and " + ar[commaUntil];

      return ret ;
  },  
查看更多
女痞
6楼-- · 2019-09-26 08:11

This one is pretty twisted :

function format(input, separator) {
    var idx = input.indexOf(separator || ' ');
    if (idx === -1) return input;
    return input.substring(0, idx++) + (
        input.indexOf(separator || ' ', idx) === -1 ? ' and ' : ', '
    ) + (
        format(input.slice(idx), separator)
    );
}

Fails to handle consecutive and trailing separators though :

format('1|2||3|', '|'); // "1, 2, , 3 and "
查看更多
登录 后发表回答