【问题标题】:Is there an indexOf in javascript to search an array with custom compare functionjavascript中是否有indexOf来搜索具有自定义比较功能的数组
【发布时间】:2012-09-03 14:41:22
【问题描述】:

我需要数组中第一个值的 索引,它与自定义比较函数匹配。

非常好的underscorej 有一个“find”函数,它返回第一个函数返回 true 的值,但我需要它来返回索引。是否有可用的 indexOf 版本,我可以在其中传递用于比较的函数?

感谢您的任何建议!

【问题讨论】:

  • 我认为你的方法在这里是错误的。您不希望功能修改默认功能(重载===),您需要自己的功能(例如;myIndexOf)。前者比后者更具破坏性和危险性。
  • 如果你的目标环境支持 ES2015(或者你有一个转译步骤,例如使用 Babel),你可以使用原生的 Array.prototype.findIndex()。

标签: javascript underscore.js indexof


【解决方案1】:

下面是下划线的方法——这增加了核心下划线函数,增加了一个接受迭代器函数的函数:

// save a reference to the core implementation
var indexOfValue = _.indexOf;

// using .mixin allows both wrapped and unwrapped calls:
// _(array).indexOf(...) and _.indexOf(array, ...)
_.mixin({

    // return the index of the first array element passing a test
    indexOf: function(array, test) {
        // delegate to standard indexOf if the test isn't a function
        if (!_.isFunction(test)) return indexOfValue(array, test);
        // otherwise, look for the index
        for (var x = 0; x < array.length; x++) {
            if (test(array[x])) return x;
        }
        // not found, return fail value
        return -1;
    }

});

_.indexOf([1,2,3], 3); // 2
_.indexOf([1,2,3], function(el) { return el > 2; } ); // 2

【讨论】:

  • 下划线太过分了。您可能希望将整个内容包装在 IIFE 中:您刚刚引入了对全局变量的依赖关系。此外,如果您的代码中有错误,您已经使用 _.indexOf 污染了所有代码。
  • @mintsauce - OP 引用了下划线,这就是我提供基于下划线的解决方案的原因。 W/r/t 全局引用,这是一个 sn-p,而不是一个插入模块;用户的工作是对其进行包装或以适合其应用程序的方式对其进行设置。 W / r / t bug - 真的,这就是我更喜欢编写无错误代码的原因:)。
  • 注意:underscore.js 在原帖和回答后添加了findIndex 函数。
【解决方案2】:

在 ECMAScript 2015 中有 a standard function 用于 Array.prototype.findIndex()。目前它已在除 Internet Explorer 之外的所有主要浏览器中实现。

这是一个 polyfill,由 Mozilla Developer Network 提供:

// https://tc39.github.io/ecma262/#sec-array.prototype.findIndex
if (!Array.prototype.findIndex) {
  Object.defineProperty(Array.prototype, 'findIndex', {
    value: function(predicate) {
     // 1. Let O be ? ToObject(this value).
      if (this == null) {
        throw new TypeError('"this" is null or not defined');
      }

      var o = Object(this);

      // 2. Let len be ? ToLength(? Get(O, "length")).
      var len = o.length >>> 0;

      // 3. If IsCallable(predicate) is false, throw a TypeError exception.
      if (typeof predicate !== 'function') {
        throw new TypeError('predicate must be a function');
      }

      // 4. If thisArg was supplied, let T be thisArg; else let T be undefined.
      var thisArg = arguments[1];

      // 5. Let k be 0.
      var k = 0;

      // 6. Repeat, while k < len
      while (k < len) {
        // a. Let Pk be ! ToString(k).
        // b. Let kValue be ? Get(O, Pk).
        // c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)).
        // d. If testResult is true, return k.
        var kValue = o[k];
        if (predicate.call(thisArg, kValue, k, o)) {
          return k;
        }
        // e. Increase k by 1.
        k++;
      }

      // 7. Return -1.
      return -1;
    },
    configurable: true,
    writable: true
  });
}

【讨论】:

  • findIndex 实际上同时符合 EcmaScript 2015 标准,请参阅上面@Husky 提供的链接
  • Underscore 还有一个 findIndex() 函数,以防浏览器不支持它
【解决方案3】:

你可以这样做:

Array.prototype.myIndexOf = function(f)
{
    for(var i=0; i<this.length; ++i)
    {
        if( f(this[i]) )
            return i;
    }
    return -1;
};

关于 Christian 的评论:如果您使用具有 不同 相同签名和不同功能的自定义方法覆盖标准 JavaScript 方法,则很可能会发生坏事。如果您要引入可能依赖于原始库(例如 Array.proto.indexOf)的 3rd 方库,则尤其如此。所以是的,你可能想给它起个别的名字。

【讨论】:

  • 感谢您的关注。我也相信第二次机会。 ;) 请强调为什么Array.prototype.indexOf(function) 是错误的方法,我会给你一个赞成票。
  • 感谢这个。我可以在不将其添加到 Array.prototype 的情况下使用它。
  • 最好不要添加到 Array.prototype 中,除非您为标准中但特定实现不支持的函数提供 shim。
【解决方案4】:

正如其他人所指出的,很容易自己推出,您可以针对您的特定用例保持简短和简单:

// Find the index of the first element in array
// meeting specified condition.
//
var findIndex = function(arr, cond) {
  var i, x;
  for (i in arr) {
    x = arr[i];
    if (cond(x)) return parseInt(i);
  }
};

var moreThanTwo = function(x) { return x > 2 }
var i = findIndex([1, 2, 3, 4], moreThanTwo)

或者,如果您是 CoffeeScripter:

findIndex = (arr, cond) ->
  for i, x of arr
    return parseInt(i) if cond(x)

【讨论】:

    【解决方案5】:

    javascript 数组方法 filter 返回从传递的函数返回 true 的数组子集。

    var arr= [1, 2, 3, 4, 5, 6],
    first= arr.filter(function(itm){
        return itm>3;
    })[0];
    alert(first);
    
    if you must support IE before #9 you can 'shim' Array.prototype.filter-
    
    Array.prototype.filter= Array.prototype.filter || function(fun, scope){
        var T= this, A= [], i= 0, itm, L= T.length;
        if(typeof fun== 'function'){
            while(i<L){
                if(i in T){
                    itm= T[i];
                    if(fun.call(scope, itm, i, T)) A[A.length]= itm;
                }
                ++i;
            }
        }
        return A;
    }
    

    【讨论】:

    • 谢谢,构建一个新的子集数组可能会降低性能 - 你怎么看?
    • 我认为这是最简单、最优雅的答案。不需要定义任何新方法、下划线或 polyfill 或任何东西。我说的是简单性,而不是性能。
    【解决方案6】:

    这样的查找功能怎么样?

    (function () {
      if (!Array.prototype._find) {
        Array.prototype._find = function (value) {
          var i = -1, j = this.length;
          if (typeof(value)=="function") 
             for(; (++i < j) && !value(this[i]););
          else
             for(; (++i < j) && !(this[i] === value););
    
          return i!=j ? i : -1;
        }
      }
    }());
    

    【讨论】:

      【解决方案7】:

      nrabinowitz 的 code 的咖啡脚本版本来了。

      # save a reference to the core implementation
      indexOfValue = _.indexOf
      
      # using .mixin allows both wrapped and unwrapped calls:
      # _(array).indexOf(...) and _.indexOf(array, ...)
      _.mixin ({
          # return the index of the first array element passing a test
          indexOf: (array, test) ->
              # delegate to standard indexOf if the test isn't a function
              if (!_.isFunction(test))
                  return indexOfValue(array, test)
              # otherwise, look for the index
              for item, i in array
                  return i if (test(item))
              # not found, return fail value
              return -1
      })
      

      【讨论】:

        【解决方案8】:

        使用下划线,我想出了一些从他们使用 _.any 的 find 实现中复制的东西:

        findIndex = function (obj, iterator, context) {
            var idx;
            _.any(obj, function (value, index, list) {
                if (iterator.call(context, value, index, list)) {
                    idx = index;
                    return true;
                }
            });
            return idx;
        };
        

        你觉得怎么样 - 你有更好的解决方案吗?

        【讨论】:

        • 我不知道_.any 是什么意思。我猜你正在使用一些框架?如果是这样,请善待我们,告诉我们你的秘密;)。
        • 你不应该对any使用额外的功能,它会减慢方法的速度。只需使用一个简单的 for 循环
        猜你喜欢
        • 2016-06-17
        • 1970-01-01
        • 2015-10-04
        • 2019-11-22
        • 2022-12-14
        • 2023-03-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多