Skip navigation
Currently Being Moderated

Index of array based on value

Jul 24, 2012 12:47 AM

Is there any inbuilt method to get the index of a \n array by its value (in JSX). It seems that indexOf doesn't work here as in javascript..

 
Replies
  • Currently Being Moderated
    Jul 24, 2012 12:50 AM   in reply to Shadowfax

    nope!

    i use this:

    Array.prototype.findIndex = function (value)
    {
              var ctr = "";
              for (var i = 0; i < this.length; i++)
              {
                        // use === to check for Matches. ie., identical (===), ;
                        if (this[i] == value)
                        {
                                  return i;
                        }
              }
              return ctr;
    };
    

     

    don't know from who i got this snippet, but lots and lots of thanks

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 25, 2012 12:21 AM   in reply to Shadowfax

    JSX is based on an older version of ECMAScript. indexOf did not used to exist for Arrays.

     

    Here's an implementation that's equivalent to the javascript version:

     

    Array.prototype.indexOf = function (elem, fromIndex){
        fromIndex = fromIndex || 0;
        for(var i = fromIndex; i < this.length; i++){
            if(this[i] == elem){
                return i;
            }
        }
        return -1;
    }
    

     

    There's a problem with Vamitul's version in that it returns a blank string if the index is not found, but it returns a number if it is. That makes it difficult to determine if the index is found or not. With the standard implementation you test for index < 0 to determine if the element is not found.

     

    Harbs

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 25, 2012 12:30 AM   in reply to Harbs.

    Here's a more robust version which supports negative indices as well:

     

    Array.prototype.indexOf = function (elem, fromIndex){
        fromIndex = Number(fromIndex) || 0;
        var len = this.length;
        if (fromIndex < 0){
            fromIndex += len;
        }
        for(var i=fromIndex;i<this.length;i++){
            if(this[i] == elem){
                return i;
            }
        }
        return -1;
    }
    
     
    |
    Mark as:
  • Currently Being Moderated
    Jul 25, 2012 3:59 AM   in reply to Shadowfax

    FWIW, here's the implementation recommended by Mozilla (which has more robust error checking) :

     

    if (!Array.prototype.indexOf) {
        Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) {  
            "use strict";
            if (this == null) {
                throw new TypeError();
            }
            var t = Object(this);
            var len = t.length >>> 0;
            if (len === 0) {
                return -1;
            }
            var n = 0;
            if (arguments.length > 0) {
                n = Number(arguments[1]);
                if (n != n) { // shortcut for verifying if it's NaN
                    n = 0;
                } else if (n != 0 && n != Infinity && n != -Infinity) {
                    n = (n > 0 || -1) * Math.floor(Math.abs(n));
                }
            }
            if (n >= len) {
                return -1;
            }
            var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);
            for (; k < len; k++) {
                if (k in t && t[k] === searchElement) {
                    return k;
                }
            }
            return -1;
        }
    }
    
     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points