๐Ÿ›น [JS] | ๐ŸŒŸ ์ฝœ๋ฐฑ ํ•จ์ˆ˜ , arr ๋ฉ”์†Œ๋“œ๋“ค์˜ ์›๋ฆฌ

๐Ÿ”น ๐ŸŒŸ ์ฝœ๋ฐฑ ํ•จ์ˆ˜

๋‹ค๋ฅธ ํ•จ์ˆ˜์— ์ธ์ˆ˜๋กœ ๋„˜๊ฒจ์ง€๋Š” ํ•จ์ˆ˜๊ฐ€ ๋ฐ”๋กœ ์ฝœ๋ฐฑํ•จ์ˆ˜์˜ˆ์š”.

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋™์ž‘์›๋ฆฌ์™€ ์ฝœ๋ฐฑํ•จ์ˆ˜ ํ”„๋กœ๋ฏธ์Šค async ๊ฐ„๋‹จ ์ •๋ฆฌ


๐Ÿ”น forEach() ์›๋ฆฌ

 var arr = {
        0: 10,
        1: 20,
        2: 30,
        3: 40,
        length: 4,
        forEach: function (print) {
          for (var i = 0; i < arr.length; i++) {
            print(this[i]);
          }
        },
      };

function print(item) { console.log("item : " + item); }


๐Ÿ”น filter() ์›๋ฆฌ

      var arr = {
        0: 1,
        1: 2,
        2: 3,
        3: 4,
        4: 5,
        5: 6,
        6: 7,
        7: 8,

        length: 8,
        filter: function (printB) {
          var newArr = [];
          for (var i = 0; i < this.length; i++) {
            var val = this[i];
            if (printB(val)) {
              newArr.push(val);
            }
          }
          return newArr;
        },
      };

      var resultM = arr.filter(function (item) {
        return item % 2;
      });

      console.log("resultM : " + resultM);


๐Ÿ”น arr.map( )

  • map (์—ฌ๊ธฐ ์•ˆ์—์„œ function์„ ๋งŒ๋“ค์–ด์„œ ์ฃผ๋กœ ์‚ฌ์šฉ)
  • var result = arr.map(function(item) { return item - 2; });
    โ†• ๊ฐ™๋‹ค
    var resultF = arr.map(myMap); function myMap(item) { return item - 2; }
var arr = [10, 20, 30, 40];

var result = arr.map(function (item) { return item * 2; });

function myMap(item) { return item - 2; }

var resultF = arr.map(myMap);

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ