Lef*_*eff 18 javascript arrays
我有一个包含很多项目的数组,我正在创建它们的列表.我在考虑将列表分页.我想知道如何在数组中的某些部分启动forEach或for循环,index在我的示例中,每个页面上列表中的项目数量,以便我不需要在每个循环中迭代整个数组?
arr.forEach(function (item) {
someFn(item);
})
for (var i = 0, len = arr.length; i < len; i++) {
someFn(arr[i]);
}
Run Code Online (Sandbox Code Playgroud)
Nin*_*olz 12
你可以使用数组的副本 Array#slice
该
slice()方法将数组的一部分的浅表副本返回到从开始到结束选择的新数组对象(不包括结束).原始数组不会被修改.
array.slice(10, 20).forEach(someFn); // only for functions which respects API of forEach*
Run Code Online (Sandbox Code Playgroud)
*回调参数
或者您可以从给定索引开始并以给定索引结束.
for (var i = 10, len = Math.min(20, arr.length); i < len; i++) {
someFn(arr[i]);
}
Run Code Online (Sandbox Code Playgroud)
同
Math.min(20, arr.length)
Run Code Online (Sandbox Code Playgroud)
如果数组小于给定值,则返回一个值20.例如,如果数组只有索引0 ... 14,则得到结果15.
forEach不提供该功能,不.所以你的选择是:
这是#4作为Array.prototype扩展(当然是不可枚举的;添加可枚举属性来Array.prototype破坏大量代码); 之后它是一个独立的版本,当添加时Array.prototype不合适:
// 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);Run Code Online (Sandbox Code Playgroud)
.as-console-wrapper {
max-height: 100% !important;
}Run Code Online (Sandbox Code Playgroud)
再次注意,这是一个不可枚举的属性,如果你要添加任何东西,这是至关重要的Array.prototype(否则,你会破坏很多代码).
您不会在库中执行此操作以供其他人使用,您只需要一个独立的功能:
// 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);Run Code Online (Sandbox Code Playgroud)
.as-console-wrapper {
max-height: 100% !important;
}Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20917 次 |
| 最近记录: |