【问题标题】:Getting the first and last element of an array in CoffeeScript在 CoffeeScript 中获取数组的第一个和最后一个元素
【发布时间】:2012-07-17 00:40:10
【问题描述】:

如果说我有一个数组,我会遍历数组,但对第一个和最后一个元素做一些不同的事情。我该怎么做?

以下面的代码为例,如何提醒元素ae

array = [a,b,c,d,e]
for element in array
  console.log(element)

谢谢。

【问题讨论】:

    标签: arrays coffeescript


    【解决方案1】:

    你可以只使用:

    [..., last] = array
    

    【讨论】:

    • 请编辑您的答案,并解释为什么这是一个好的解决方案。
    • 这是不言自明的。
    【解决方案2】:

    访问数组的第一个和最后一个元素的原始方法实际上与 JS 中的相同:使用索引 0length - 1

    console.log array[0], array[array.length - 1]
    

    CoffeeScript 让您可以编写一些不错的数组解构表达式:

    [first, mid..., last] = array
    console.log first, last
    

    但如果你不打算使用中间元素,我认为不值得。

    Underscore.js 有一些辅助方法 firstlast 可以使它更像英语(我不想使用“不言自明”这个短语,因为我认为任何程序员都会理解数组索引) .如果您不想使用 Underscore 并且不介意污染全局命名空间(这是其他库,如 Sugar.js 所做的),它们很容易添加到 Array 对象中:

    Array::first ?= (n) ->
      if n? then @[0...(Math.max 0, n)] else @[0]
    
    Array::last ?= (n) ->
      if n? then @[(Math.max @length - n, 0)...] else @[@length - 1]
    
    console.log array.first(), array.last()
    

    更新

    此函数还允许您获取数组中的第 n 个元素。如果您不需要该功能,那么实现会简单得多(基本上只是 else 分支)。

    更新 2

    CoffeeScript >= 1.7 让你写作:

    [first, ..., last] = array
    

    不使用中间元素生成不必要的数组:D

    【讨论】:

    • 虽然你的 firstlast 原型方法优雅且有用,但应该注意它们总是返回 arrays,这与 Sugar.js 的行为不同和 underscore.js,默认情况下只返回请求的元素,并且仅在指定 n 时返回一个数组(即使 n 等于 1)。因此,我建议您将它们替换为以下内容:Array::first ?= (n) -> if n? then @splice 0, n else @[0]Array::last ?= (n) -> if n? then @splice (if n >= @length then 0 else @length - n) else @[@length-1]
    • 谢谢@mklement0!我完全忘记了只返回第一个或最后一个元素的简单情况:(。我根据您的评论更新了答案,但使用Array#slice 而不是Array#splice 以避免修改原始数组。
    • 感谢您更新您的帖子并纠正拼接/切片混淆。另一个可能有意义的调整:排除负索引:Array::first ?= (n) -> if n? then (if n <= 0 then [] else @slice 0, n) else @[0]Array::last ?= (n) -> if n? then (if n <= 0 then [] else @slice -n) else @[@length-1]
    • @mklement0,完成(y)。我正在考虑不要使实现更加复杂,但后来认为使用Math.max 可以帮助避免使用嵌套的ifs,并且会为maxmin 产生一个相当对称的实现,所以就是这样:)
    【解决方案3】:

    您可以使用带有 splat 的数组 destructuring 来检索第一个和最后一个元素:

    [first, ..., last] = array
    

    CoffeeScript >= 1.7.0 支持这种 splat 用法。

    【讨论】:

    • 请注意,在 CoffeeScript >= 1.7 上,您可以编写 [first, ..., last] = array,它不会生成不必要的 Array#slice 调用:D
    【解决方案4】:

    最短的方法在这里
    array[-1..]
    看到这个帖子
    https://github.com/jashkenas/coffee-script/issues/156

    【讨论】:

    • 我赞成这个,尽管这会给你一个包含在一个新数组中的数组的最后一个元素。正确的代码应该是array[-1..][0]
    • 这是错误的。将其更新为 drinchev 的建议值得一票。
    【解决方案5】:

    您可以使用slice 获取最后一个元素。在 javascript 中,slice 可以传递负数,例如 -1 作为参数。

    例如:

    array = [1, 2, 3 ]
    console.log "first: #{array[0]}"
    console.log "last: #{array[-1..][0]}"
    

    编译成

    var array;
    array = [1, 2, 3];
    console.log("first: " + array[0]);
    console.log("last: " + array.slice(-1)[0]);
    

    【讨论】:

      【解决方案6】:

      在使用 Coffeescript 的 for...in 遍历数组时,您可以获得当前 elementelementindex。请参阅以下代码,将special_process_for_elementnormal_process_for_element 替换为您的代码。

      array = [a, b, c, d]
      FIRST_INDEX = 0
      LAST_INDEX = array.length - 1
      
      for element, index in array
          switch index
              when FIRST_INDEX, LAST_INDEX
                  special_process_for_element
              else
                  normal_process_for_element
      

      sample

      这是working code

      【讨论】:

        猜你喜欢
        • 2012-08-18
        • 2013-12-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-13
        • 2012-04-21
        • 2013-01-14
        • 2023-04-09
        • 2017-11-15
        相关资源
        最近更新 更多