#295 doc
NoiseEee

Docs should specify that Array#first returns array[0]

Reported by NoiseEee | August 20th, 2008 @ 06:46 PM

Hello

It seems array.first() only looks for array[0]. Example below:


var bigArr = new Array();
bigArr[1] = 'dog';
bigArr[2] = 'cat';
bigArr.inspect();
bigArr.last();
alert(bigArr.first());
bigArr[0] = 'bird';
alert(bigArr.first());

Comments and changes to this ticket

  • Nick Stakenburg

    Nick Stakenburg August 20th, 2008 @ 07:28 PM

    The first items in your case is undefined, so that is as expected.

  • John-David Dalton

    John-David Dalton August 20th, 2008 @ 07:52 PM

    • → Tag changed from “array array.first function needs_patch” to “array array.first needs_patch”
    • → State changed from “new” to “invalid”

    try

    
    myArray.find(function(n){ return !Object.isUndefined(n) });
    
    // OR
    Array.prototype.firstDefined = function() {
      return this.find(function(n){ return !Object.isUndefined(n) });
    };
    
    myArray.firstDefined();
    
    
  • NoiseEee

    NoiseEee August 20th, 2008 @ 08:00 PM

    With all due respect, I really think that your "firstDefined" function should be the actual function called by "array.first();"

    According to the docs, array.first() "Returns the first item in the array, or undefined if the array is empty."

    From the docs, I would expect array.first() to return the first item in the array... otherwise what is the point of array.first() at all? Why would anyone ever call it in the first place instead of MyArray[0]???

    Array.first() being what you defined above (firstDefined) becomes actually useful with clear benefits - as it is now, its a truly useless function.

  • John-David Dalton

    John-David Dalton August 20th, 2008 @ 08:05 PM

    I take it as more of a companion to Array#last.

  • NoiseEee

    NoiseEee August 20th, 2008 @ 08:38 PM

    Yes, it seems I'm getting nowhere fast with my request.

    Perhaps the docs should change to reflect its actual use - "returns array[0]" instead of "returns the first item in an array".

    Again, the docs suggest that if "undefined" is returned, that the array itself is empty. And again, your "firstDefined" function above would really be useful as "array.first()" because it would return the first item in the array, not just array[0]

    I won't harp on it any longer... maybe we can all be happy by adding a firstDefined() method to Array, which would actually get used!!

  • John-David Dalton

    John-David Dalton August 20th, 2008 @ 09:10 PM

    • → Assigned user changed from “” to “Tobie Langel”
    • → State changed from “invalid” to “enhancement”
    • → Title changed from “Array.first() Returns Array[0] only, not first item” to “Array#first should filter to first defined item”
    
    The first item in the array is undefined.
    So it DID return the correct value.
    
    If you want to know if an array is empty check its
    myArr.length
    
    If you want to use the custom method then go for it:
    
    
    Object.extend(Array.prototype, {
      first: function() {
        return this.find(function(n){ return !Object.isUndefined(n) });
      },
      last: function() {
        return this.reverse().first();
      }
    });
    

    I am changing this ticket to a feature request.

  • Juriy Zaytsev

    Juriy Zaytsev August 20th, 2008 @ 09:43 PM

    • → Title changed from “Array#first should filter to first defined item” to “Array.first() Filter to first defined value”

    @NoiseEee

    This behavior is intentional. It looks like ECMAScript (and so JavaScript) defines first item as the one with index === 0. e.g. as per specs, Array.prototype.shift is meant to return "first item in an array":

    
    15.4.4.9  Array.prototype.shift()
    
        The first element of the array is removed from the array and returned.
    

    And if we run a simple test, we can see how shift returns undefined:

    
    var arr = [];
    arr[1] = 'foo';
    arr[2] = 'bar';
    arr.shift() === undefined; // true
    

    I agree that the docs could be a little more clear about this and I agree that the only purpose of Array#first is consistency (i.e. the opposite of Array#last) and syntax sugar.

  • Juriy Zaytsev

    Juriy Zaytsev August 20th, 2008 @ 09:43 PM

    • → Title changed from “Array.first() Filter to first defined value” to ““Array#first should return first defined item””

    @NoiseEee

    On the other hand, if you don't mind O(N) complexity, using compact should solve this issue:

    
    var arr = [];
    arr[100] = 'foo';
    arr[200] = 'bar';
    arr.compact().first(); // 'foo'
    
  • NoiseEee

    NoiseEee August 20th, 2008 @ 10:17 PM

    • → Assigned user cleared.

    @ Juriy

    re: "compact"

    Good idea.

  • Tobie Langel

    Tobie Langel August 21st, 2008 @ 12:40 AM

    • → Tag cleared.
    • → State changed from “enhancement” to “doc”
    • → Title changed from ““Array#first should return first defined item”” to “Docs should specify that Array#first returns array[0]”

    Changing that to a documentation issue.

Please Login or create a free account to add a new comment.

You can update this ticket by sending an email to from your email client. (help)

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

The Prototype JavaScript library.

Shared Ticket Bins