Start from second item in forEach loop

后端 未结 1 1514
轮回少年
轮回少年 2021-01-18 16:32

I need to start from second item in array. In order to save correct context I need to use forEach instead of simple for loop.

I\'ve done it

1条回答
  •  执念已碎
    2021-01-18 17:22

    You can't tell forEach where to start, no, but you can ignore the calls you don't want:

    inputArr.forEach((value, index) => {
        if (index < 1) return;
        // Code from here onward will only run for entries that aren't
        // the first entry
    });
    

    Or if you're not worried about copying most of the array, you can always use slice:

    inputArr.slice(1).forEach(value => {
        // ...
    });
    

    You could also define your own forEach-style function accepting a starting index, if you liked, making it non-enumerable and choosing the name carefully to avoid conflicts.

    It's also perhaps worth noting that since you're using ES2015, some of the reasons for using forEach go away a bit thanks to block scope. for is still a bit more verbose than forEach with an arrow function, but lets you start and end and increment by whatever you like:

    for (let i = 1; i < inputArr.length; ++i) {
        // ...`i` and also any `let` or `const` here are scoped to
        // this specific loop iteration...
    }
    

    The part about i above is primarily a good thing, but also has a slight performance impact (at least for now) since a new i has to be created for each iteration. Not that that performance impact usually matters at all, though, and it won't be as big as calling a function a'la forEach.

    Gratuitous example of the above:

    const inputArr = ['a', 'b', 'c', 'd'];
    for (let i = 1; i < inputArr.length; ++i) {
      // A closure to emphasize that each `i` is distinct
      setTimeout(() => {
        console.log("inputArr[" + i + "] = " + inputArr[i]);
      }, 0);
    }

    (Normally I'd use a template literal there, but wanted to avoid giving the impression the i behavior related to that.)

    0 讨论(0)
提交回复
热议问题