Angular中与jQuery $.map相当的方法是什么?

36

我正在从依赖jQuery转向使用AngularJS构建应用程序。在许多地方都推荐不要混合使用jQuery和Angular代码。

但是,有一件事情让我很想念,那就是jQuery的$ .map函数,用于数组操作。我知道可以使用本地JavaScript map函数重写此功能,但部分浏览器不支持(特别是IE < v9)。

所以,是否有Angular的等效方法,或者我应该回到使用for (var x = 0; x < foo; x += 1) {...}来避免使用jQuery?

更新有时只需要知道搜索什么。Bergie说“寻找polyfills”。这是一个参考指南(来自Modernizr团队),其中包括许多资源,可让现代代码在旧浏览器上运行:HTML5跨浏览器Polyfills


1
你可以只包含一个ES5 polyfill,它们对于那些数组迭代函数来说是简单而精确的。 - Bergi
5
Angular 不是一个 JavaScript 库,而是一个用于构建单页应用程序的框架。虽然它确实有一些实用函数(参见 angular.forEach),但不会指导如何处理低级 JavaScript 操作,这正是 Lo-Dash/Underscore 等库所专门用来处理的。 - Stewie
除了@Stewie提到的之外,过了一段时间,Angular实际上将一堆jquery倒进去并称其为JQLite - 所以当你使用Angular时,无论你喜不喜欢,你都会得到很多jquery。还是用它吧。虽然在像Angular的forEach这样的东西中仍然存在很多浪费,如果有的话,最好利用jquery。http://docs.angularjs.org/api/angular.element - Chris Moschini
1
他们警告不要将与Angular混合使用的jQuery类型是直接操纵DOM的类型,而不是将函数映射到JavaScript数组的实用程序。 - flup
2个回答

26

请查看这里:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/map

对于不支持的浏览器,Mozilla提供了一个Array.map的兼容性解决方案。

if (!Array.prototype.map) {
  Array.prototype.map = function(callback, thisArg) {

    var T, A, k;

    if (this == null) {
      throw new TypeError(" this is null or not defined");
    }

    // 1. Let O be the result of calling ToObject passing the |this| value as the argument.
    var O = Object(this);

    // 2. Let lenValue be the result of calling the Get internal method of O with the argument "length".
    // 3. Let len be ToUint32(lenValue).
    var len = O.length >>> 0;

    // 4. If IsCallable(callback) is false, throw a TypeError exception.
    // See: http://es5.github.com/#x9.11
    if (typeof callback !== "function") {
      throw new TypeError(callback + " is not a function");
    }

    // 5. If thisArg was supplied, let T be thisArg; else let T be undefined.
    if (thisArg) {
      T = thisArg;
    }

    // 6. Let A be a new array created as if by the expression new Array(len) where Array is
    // the standard built-in constructor with that name and len is the value of len.
    A = new Array(len);

    // 7. Let k be 0
    k = 0;

    // 8. Repeat, while k < len
    while(k < len) {

      var kValue, mappedValue;

      // a. Let Pk be ToString(k).
      //   This is implicit for LHS operands of the in operator
      // b. Let kPresent be the result of calling the HasProperty internal method of O with argument Pk.
      //   This step can be combined with c
      // c. If kPresent is true, then
      if (k in O) {

        // i. Let kValue be the result of calling the Get internal method of O with argument Pk.
        kValue = O[ k ];

        // ii. Let mappedValue be the result of calling the Call internal method of callback
        // with T as the this value and argument list containing kValue, k, and O.
        mappedValue = callback.call(T, kValue, k, O);

        // iii. Call the DefineOwnProperty internal method of A with arguments
        // Pk, Property Descriptor {Value: mappedValue, : true, Enumerable: true, Configurable: true},
        // and false.

        // In browsers that support Object.defineProperty, use the following:
        // Object.defineProperty(A, Pk, { value: mappedValue, writable: true, enumerable: true, configurable: true });

        // For best browser support, use the following:
        A[ k ] = mappedValue;
      }
      // d. Increase k by 1.
      k++;
    }

    // 9. return A
    return A;
  };      
}

太好了!直接而准确。 - Ben Jacobs
1
OP在问题中有一个指向同一页的链接...但也许他没有看到这个补丁? - the system
@thesystem:我承认我匆忙地粗略地看了一下。 - Ben Jacobs
1
“他们”是谁?我在Angular源代码中没有看到这段代码。 - kilianc
@kilianc,“They”指的是Mozilla开发者网络,链接指向他们的页面。你看到的polyfill是他们建议用于旧浏览器的工具。 - Edward Brey
@EdwardBrey 好的,那个 "they" 看起来像是 Angular 的人 :) - kilianc

13

不,Angular没有相应的等效物。正如您发现的那样,不需要退回到编写命令式代码。

相反,只需使用内置在JavaScript中的 map 函数即可。如果需要支持IE8,请在脚本开头插入 polyfill


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接