Javascript - how to start forEach loop at some index

前端 未结 6 1635
予麋鹿
予麋鹿 2021-01-04 00:31

I have an array with alot of items, and I am creating a list of them. I was thinking of paginating the list. I wonder how can I start a forEach or for

6条回答
  •  感动是毒
    2021-01-04 00:57

    forEach doesn't offer that feature, no. So your choices are:

    1. A simple for loop

    2. Ignoring the indexes you don't want to handle (as in Kind user's answer)

    3. Using slice (as in Nina's answer)

    4. Writing your own function

    Here's #4 as an Array.prototype extension (non-enumerable, of course; adding enumerable properties to Array.prototype breaks a lot of code); after it is a standalone version for when adding to Array.prototype isn't appropriate:

    // Giving ourselves the function
    Object.defineProperty(Array.prototype, "myEach", {
      value: function(from, to, callback, thisArg) {
        if (typeof from === "function") {
          thisArg = callback;
          callback = to;
          to = from;
          from = 0;
        }
        if (typeof to === "function") {
          thisArg = callback;
          callback = to;
          to = this.length;
        }
        for (var n = from; n < to; ++n) {
          callback.call(thisArg, this[n], n, this);
        }
      }
    });
    
    // Using it:
    var arr = ["zero", "one", "two", "three", "four", "five", "six", "seven"];
    console.log("*** From 3:");
    arr.myEach(3, function(e) { console.log(e); });
    console.log("*** From 3 (inclusive) to 5 (exclusive):");
    arr.myEach(3, 5, function(e) { console.log(e); });
    console.log("*** All:");
    arr.myEach(function(e) { console.log(e); });
    console.log("*** Check thisArg handling on 0-2:");
    var o = {answer: 42};
    arr.myEach(0, 2, function(e) {
      console.log(e + " (this.answer = " + this.answer + ")");
    }, o);
    .as-console-wrapper {
      max-height: 100% !important;
    }

    Again note that that's a non-enumerable property, which is vital if you ever add anything to Array.prototype (otherwise, you break a lot of code).

    You wouldn't do that in a library to be consumed by others, you'd just have a standalone function:

    // Giving ourselves the function
    function myEach(array, from, to, callback, thisArg) {
      if (typeof from === "function") {
        thisArg = callback;
        callback = to;
        to = from;
        from = 0;
      }
      if (typeof to === "function") {
        thisArg = callback;
        callback = to;
        to = array.length;
      }
      for (var n = from; n < to; ++n) {
        callback.call(thisArg, array[n], n, array);
      }
    }
    
    // Using it:
    var arr = ["zero", "one", "two", "three", "four", "five", "six", "seven"];
    console.log("*** From 3:");
    myEach(arr, 3, function(e) {
      console.log(e);
    });
    console.log("*** From 3 (inclusive) to 5 (exclusive):");
    myEach(arr, 3, 5, function(e) {
      console.log(e);
    });
    console.log("*** All:");
    myEach(arr, function(e) {
      console.log(e);
    });
    console.log("*** Check thisArg handling on 0-2:");
    var o = {answer: 42};
    myEach(arr, 0, 2, function(e) {
      console.log(e + " (this.answer = " + this.answer + ")");
    }, o);
    .as-console-wrapper {
      max-height: 100% !important;
    }

提交回复
热议问题