Remove empty strings from array while keeping record Without Loop?

后端 未结 7 1567
臣服心动
臣服心动 2020-11-28 20:55

This question was asked here: Remove empty strings from array while keeping record of indexes with non empty strings

If you\'d notice the given as @Baz layed it out;

相关标签:
7条回答
  • 2020-11-28 21:40

    PLEASE NOTE: The documentation says:

    filter is a JavaScript extension to the ECMA-262 standard; as such it may not be present in other implementations of the standard. You can work around this by inserting the following code at the beginning of your scripts, allowing use of filter in ECMA-262 implementations which do not natively support it. This algorithm is exactly the one specified in ECMA-262, 5th edition, assuming that fn.call evaluates to the original value of Function.prototype.call, and that Array.prototype.push has its original value.

    So, to avoid some heartache, you may have to add this code to your script At the beginning.

    if (!Array.prototype.filter) {
      Array.prototype.filter = function (fn, context) {
        var i,
            value,
            result = [],
            length;
            if (!this || typeof fn !== 'function' || (fn instanceof RegExp)) {
              throw new TypeError();
            }
            length = this.length;
            for (i = 0; i < length; i++) {
              if (this.hasOwnProperty(i)) {
                value = this[i];
                if (fn.call(context, value, i, this)) {
                  result.push(value);
                }
              }
            }
        return result;
      };
    }
    
    0 讨论(0)
提交回复
热议问题