Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Feature proposal: Array#indexOf(iterator, i, context)
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  1 message - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Rafael Raposo  
View profile  
 More options Jul 21 2011, 2:47 pm
From: Rafael Raposo <orapo...@gmail.com>
Date: Thu, 21 Jul 2011 15:47:52 -0300
Local: Thurs, Jul 21 2011 2:47 pm
Subject: Feature proposal: Array#indexOf(iterator, i, context)

Hello,

Don't know if this is going to be of any use for someone or to enter the
core, but I needed to write a new indexOf that uses an iterator to get the
index from an Array. Array#find finds the value, but I need the index so
here is the code if you want to take a look. I know that maybe Array#indexOf
will be deprecated for native code use, but I find this version  with
iterator quite useful.

(function(){
    Array.prototype._indexOf = Array.prototype.indexOf;
    function indexOf(itemIt, i, context){
        //not an iterator, uses regular indexOf
        if(!Object.isFunction(itemIt)) {
            return this._indexOf(itemIt, i);
        }

        //item as a function is an iterator
        //NaN 'i' is the context, nullifies 'i' to initialize it correctly
after
        if(!Object.isNumber(i)) {
            context = i;
            i = null;
        }

        i || (i = 0);
        var length = this.length;
        if (i < 0) i = length + i;
        for (; i < length; i++)
            if (itemIt.call(context, this[i], i)) {
                return i;
            }
        return -1;
    }
    //redefines indexOf
    Array.prototype.indexOf = indexOf;

})();

I kept the old indexOf for no-iteretor equality cases.

I know that this won't work on a function array if you want the index of a
function inside it. Maybe there should/could be an specific indexOfIt()
method instead of overriding the regular Array#indexOf method. What do you
think?

Cheers,
Rafael


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »