2017-04-08 2 views
5

मेरे पास बहुत सारी चीज़ें हैं, और मैं उनकी एक सूची बना रहा हूं। मैं सूची को पगाने की सोच रहा था। मुझे आश्चर्य है कि मैं forEach या for लूप को किसी एरे में index पर कैसे शुरू कर सकता हूं, जो कि मेरे उदाहरण में प्रत्येक पृष्ठ पर सूची में आइटमों की संख्या होगी, ताकि मुझे प्रत्येक में संपूर्ण सरणी पर फिर से चलाने की आवश्यकता न हो पाश?जावास्क्रिप्ट - कुछ इंडेक्स पर प्रत्येक लूप के लिए कैसे शुरू करें

arr.forEach(function (item) { 
    someFn(item); 
}) 


for (var i = 0, len = arr.length; i < len; i++) { 
    someFn(arr[i]); 
} 
+0

क्यों नहीं आप 'टुकड़ा()' अपने सरणी curent पृष्ठ पर आइटम प्राप्त करने के लिए 'वर ऑफसेट = currentPage * itemsPerPage; arr.slice (ऑफसेट, ऑफ़सेट + आइटमपेरपेज) .forEach (someFn); ' – Thomas

उत्तर

2

आप Array#slice

का उपयोग कर slice() विधि में एक सरणी के एक हिस्से के एक उथले प्रतिलिपि रिटर्न द्वारा सरणी की एक प्रति, इस्तेमाल कर सकते हैं प्रारंभ से अंत तक चयनित एक नया सरणी ऑब्जेक्ट (अंत शामिल नहीं है)। मूल सरणी संशोधित नहीं की जाएगी।

array.slice(10, 20).forEach(someFn); // only for functions which respects API of forEach* 

* parameters for a callback

या आप एक दिए गए इंडेक्स पर किसी दिए गए सूचकांक और अंत में शुरू कर सकते हैं।

for (var i = 10, len = Math.min(20, arr.length); i < len; i++) { 
    someFn(arr[i]); 
} 

साथ

Math.min(20, arr.length) 

एक मान देता है, अगर सरणी दिया मूल्य 20 से छोटा है। उदाहरण के लिए यदि सरणी में केवल इंडेक्स 0 है ... 14, आपको परिणाम 15 के रूप में मिलता है।

+1

@Leff: सावधान रहें कि अगर 'कुछएफएन' कई तर्कों की अनुमति देता है, तो आपके कॉलबैक 'के बीच एक बड़ा अंतर है' फ़ंक्शन (आइटम) {someFn (आइटम) ;}) 'और नीना के 'EEach (someFn) 'ऊपर, क्योंकि' forEach' कॉल के लिए कई तर्क पास करता है। लेकिन अगर 'कुछ एफएन' केवल अपने पहले तर्क का उपयोग करता है, तो नीना का रास्ता तय करना है। –

+1

'लूप' के लिए 'i' और' len' पर बाउंड चेक शामिल किए जाने चाहिए। यदि ओपी को समझ में नहीं आया कि वांछित एक अनुक्रमित लूप को कैसे शुरू और बंद करना है, तो संभवतः वे उस बारे में नहीं सोचेंगे। '12 'के लिए –

+0

@ स्क्विंट, मुझे लगता है कि यह आवश्यक नहीं है, क्योंकि लंबाई की जांच गैर मौजूद वस्तुओं को फिर से शुरू करने से रोकती है। –

1

दुर्भाग्य दिया सरणी में हर तत्व से अधिक Array#forEach दोहराता है, लेकिन आप एक साधारण शर्त है जो (निर्दिष्ट सूचकांक के साथ) तत्वों को देखते हुए समारोह लागू करने के लिए निर्धारित करने के लिए आवेदन कर सकता है।

i > 3 ? someFn(item) : null; 
^ if index more than 3 - call the function 

var arr = [1,2,3,4,5,6,7]; 
 

 
function someFn(elem){ 
 
    console.log(elem); 
 
} 
 

 
arr.forEach(function(item, i) { 
 
    return i > 3 ? someFn(item) : null; 
 
})

3

forEach उस सुविधा की पेशकश नहीं करता है, नहीं।तो अपने विकल्प हैं:

  1. एक साधारण for पाश

  2. अनुक्रमित की उपेक्षा कर आप (Kind user's answer में के रूप में)

  3. (Nina's answer के रूप में)

  4. slice का उपयोग को संभालने के लिए नहीं करना चाहते हैं
  5. अपना स्वयं का फ़ंक्शन लिखना

यहां #एक्सटेंशन के रूप में # 4 है (निश्चित रूप से गैर-गणना योग्य; 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);
.as-console-wrapper { 
 
    max-height: 100% !important; 
 
}

फिर ध्यान दें कि कि एक गैर गणनीय संपत्ति है, जो महत्वपूर्ण है, तो आप कभी भी कुछ भी जोड़ने है 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);
.as-console-wrapper { 
 
    max-height: 100% !important; 
 
}

0

क्या टिप्पणी की @NinaScholz पर सोच रही थी, तो आप शायद चर का उपयोग कर सकते हैं और लूप को बदलने के बजाय उन में कोई भी परिवर्तन सेट किया जाएगा।

function someFn(item, array2){ 
 
    array2.push(item, array2); 
 
} 
 

 
var arrayItems1 = [1,2,3,4,5,6,7,8,9,10]; 
 
var arrayItems2 = []; 
 

 
var firstIndex = 1; 
 
var lastIndex = 5; 
 
var i = 0; 
 

 
for (i = firstIndex; i < lastIndex; i++){ 
 
    someFn(arrayItems1[i], arrayItems2); 
 
} 
 

 
alert(arrayItems2.join(' '));

0

आप इटरेटर पैटर्न के कार्यान्वयन किसी तरह का आवेदन कर सकता है।

var Iterator = function (list, position) { 
    return { 
    isNext: function() { 
     return position + 1 < list.length; 
    }, 
    isDefined: function() { 
     return (position < list.length && position >= 0); 
    }, 
    element: function() { 
     return list[position]; 
    }, 
    position: function() { 
     return position; 
    }, 
    moveNext: function() { 
     if (this.isNext()) { return Iterator(list, position + 1); } 
     return Iterator([], 0); 
    }  
} 

Iterator.forEach = function (action, iterator, length) { 
    var counter = 0; 
    while (counter < length && iterator.isDefined()) { 
    counter = counter + 1; 
    action(iterator.element(), iterator.position()); 
    iterator = iterator.moveNext(); 
    } 

    return iterator; 
} 

और फिर सूची पर जाने के लिए इस्तेमाल करते हैं और एक सूची अंतिम ओवर यात्रा के राज्य रखने के लिए एक iterator है।

var list = [1, 3, 5, 3, 6]; 
var iterator = Iterator(list, 0); 

iterator = Iterator.forEach(function (element, index) { 
    console.log(element, index); 
}, iterator, 3); 

//1 0 
//3 1 
//5 2 

Iterator.forEach(function (element, index) { 
    console.log(element, index);    
}, iterator, 5); 

//3 3 
//6 4 
संबंधित मुद्दे