programing

jQuery $.map의 각도 등가?

kingscode 2023. 3. 12. 12:38
반응형

jQuery $.map의 각도 등가?

jQuery에 의존하던 것에서 AngularJS에 앱을 구축하는 것으로 전환하고 있습니다.jQuery 코드와 Angular 코드를 혼재시키지 않는 좋습니다.

제가 놓치고 있는 것은 어레이의 jQuery $.map 함수입니다.네이티브 Javascript기능을 사용하여 다시 작성할 수 있다는 것은 알지만, 모든 브라우저(특히 IE < v9)에서 구현되는 것은 아닙니다.

그럼, Angular와 동등한 것이 있나요? 아니면 다시 글을 써야 하나요?for (var x = 0; x < foo; x += 1) {...}jQuery를 그만 둘 수 있게?

업데이트 검색 대상만 알면 되는 경우가 있습니다.Bergie는 '폴리 매립지를 찾아라'고 말한다.다음은 오래된 브라우저에서 최신 코드를 작동시키기 위한 참고 자료입니다.HTML5 크로스 브라우저 폴리필

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/map 를 참조해 주세요.

Mozilla는 지원되지 않는 브라우저용으로 Array.map polyfill을 제공했습니다.

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;
  };      
}

아니요, Angular에는 동등한 항목이 없습니다.그리고 당신이 발견했듯이, 당신은 명령어 코드를 다시 쓸 필요가 없습니다.

그 대신에,mapJavaScript에 직접 내장된 함수입니다.IE8 를 서포트할 필요가 있는 경우는, 스크립트의 선두에 폴리 필을 삽입합니다.

언급URL : https://stackoverflow.com/questions/15411620/angular-equivalent-of-jquery-map

반응형