"includes"功能在 Angular2 项目中的互联网探索中不起作用



我是angular2的初学者。我在我的angular2项目的功能中使用"包括"方法。但是"包括"方法在Internet Explore中不起作用。

public filterByTags(event: any) {
    this.**selectedTags**.push(event);
    console.log(this.selectedTags)
    this.data = this.**originalData**.filter(
        item => {
            return this.selectedTags.every(tag => {
                if (item.tags.length == 0) {
                    return false;
                } else {
                    return item.tags.includes(tag.id)
                }
            });
        }
    );
}

selectedtags和OriginalData都是数组。我需要检查OriginalDat数组中是否有选定的标签值?我如何使用" indexof"不使用" Inccial"

比较这两个阵列

这与Angular无关。

includes是JavaScript中数组对象的标准方法,该方法是在ES2016中引入的,因此太新了,无法得到Internet Explorer的支持。

MDN展示了一个多填充以在过时的浏览器中添加支持:

// https://tc39.github.io/ecma262/#sec-array.prototype.includes
if (!Array.prototype.includes) {
  Object.defineProperty(Array.prototype, 'includes', {
    value: function(searchElement, fromIndex) {
      if (this == null) {
        throw new TypeError('"this" is null or not defined');
      }
      // 1. Let O be ? ToObject(this value).
      var o = Object(this);
      // 2. Let len be ? ToLength(? Get(O, "length")).
      var len = o.length >>> 0;
      // 3. If len is 0, return false.
      if (len === 0) {
        return false;
      }
      // 4. Let n be ? ToInteger(fromIndex).
      //    (If fromIndex is undefined, this step produces the value 0.)
      var n = fromIndex | 0;
      // 5. If n ≥ 0, then
      //  a. Let k be n.
      // 6. Else n < 0,
      //  a. Let k be len + n.
      //  b. If k < 0, let k be 0.
      var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
      function sameValueZero(x, y) {
        return x === y || (typeof x === 'number' && typeof y === 'number' && isNaN(x) && isNaN(y));
      }
      // 7. Repeat, while k < len
      while (k < len) {
        // a. Let elementK be the result of ? Get(O, ! ToString(k)).
        // b. If SameValueZero(searchElement, elementK) is true, return true.
        if (sameValueZero(o[k], searchElement)) {
          return true;
        }
        // c. Increase k by 1. 
        k++;
      }
      // 8. Return false
      return false;
    }
  });
}

最新更新