代码之家  ›  专栏  ›  技术社区  ›  alex

如何在JavaScript中将我的数组作为一个*好的*可读列表?

  •  5
  • alex  · 技术社区  · 15 年前

    我和他们核对过了 jQuery Validation plugin .

    看起来像

    var msg = 'You may only upload files of type ' + allowedExt.join(', ');
    

    很明显,这个名单看起来并不太快。我希望它看起来更漂亮 可读的

    有什么办法吗?

    6 回复  |  直到 15 年前
        1
  •  10
  •   Community CDub    8 年前

    更简单的方法 answer posted by alex 是通过使用 .pop() 要删除最后一个元素:

    var niceList = function(array, join, finalJoin) {
        var arr = array.slice(0), last = arr.pop();
        join = join || ', ';
        finalJoin = finalJoin || ' and ';
        return arr.join(join) + finalJoin + last;    
    };
    
        2
  •  5
  •   Nick    15 年前

    公认的答案不能很好地处理一项清单。

    function niceList(array) {
      if (!array || array.length == 0) return "";
      var clone = array.slice(0);
    
      return function build() {
        if (clone.length == 1) return clone[0];
        if (clone.length == 2) return clone[0] + ' and ' + clone[1];
        return clone.shift() + ", " + build();
      }();  
    }
    
        3
  •  4
  •   Debjit    11 年前

    只是另一种方法。

    考虑的案例:

    • []->“”
    • [“”]->“”
    • [“A”]->“A”
    • [“A”,“B”]->“A和B”

    ... 等等

    function niceList(a) {
        return [
                a
    
                /* Get all the elements except the last one.
                 * If there is just one element, get that
                 */
                .slice(0, a.length - 1 || 1)
    
                /* Create a comma separated string from these elements */
                .join(", ")
            ]
            /* Process the last element (if there is one) and concat it
             * with the processed first part.
             */
            .concat(
                a
    
                /* Create a copy */
                .slice()
    
                /* Take the last element, if there is one */
                .splice(-1, Number(a.length > 1))
            )
    
            /* Join the two processed parts */
            .join(" and ");
    }
    
        4
  •  2
  •   alex    15 年前

    var niceList = function(array, join, finalJoin) {       
        join = join || ', ';
        finalJoin = finalJoin || ' and ';       
        var length = array.length;      
        return array.slice(0, length - 1).join(join) + finalJoin + array[length - 1];    
    };
    
    alert(niceList([a, b, c])); // 'a, b and c'
    
        5
  •  2
  •   Cristian Sanchez    15 年前

    因为我们显然提供了不同版本的亚历克斯的答案,这里有一个没有 join :

    function niceList(array, join, final) {
       return array.reduce(function (pv, cv, i, a) { 
          return pv + (i == a.length - 1 ? final : join) + cv; 
       });
    }; 
    

    不适用于旧浏览器等。

        6
  •  0
  •   White Elephant    15 年前

    我把你逐字逐句地写进了一个真正的HTML列表。

    var extensions = ['html', 'txt', 'png', 'jpg'];
    var extension_list = '<ul>';
    
    for(i=0; i<extensions.length; i++)
    {
        extension_list += '<li>'+extensions[i]+'</li>';
    }
    
    extension_list += '<ul>';
    
    var msg = '<p>Sorry, you can only upload the following extensions:</p>'+extension_list;