# 如何拆分通过巨大数组循环的过程？

##### 问题描述投票：0回答：1

``````populateEnrichedProductList = (id, name) => {
let html =
`<li class="list-group-item product-item-name" productName="\${name}" productId="\${id}" data-desc="" data-ingredient="" data-allergens="">` +
`\${name}` +
`<span class="error"><i class="fas fa-exclamation-triangle"></i></span>` +
'<div class="spinner">' +
'<div class="spinner-border text-warning" role="status">' +
'</div >' +
'</div>' +
'</li >';

\$('#selectedProductList').removeClass('hidden');
\$('#selectedProductList').append(html);
};
``````

``````let selectedProducts = '#selectProductForm input:checked';
\$(selectedProducts).each(function(){
let pid   = \$(this).attr('id'),
pname = \$(this).attr('name');

populateEnrichedProductList(pid, pname);
});
``````

javascript jquery arrays loops iteration
##### 1个回答
0

``````const crypto = require('crypto')

const arr = new Array(200).fill('something')
function processChunk() {
if (arr.length === 0) {
// code executed after processing the entire array
} else {
console.log('processing chunk');
// select 10 elements and remove them from the array
const subarr = arr.splice(0, 10)
for (const item of subarr) {
// perform complex processing of each of the elements
doHeavyStuff(item)
}
// put the function in the queue
setImmediate(processChunk)
}
}

processChunk()

function doHeavyStuff(item) {
crypto.createHmac('sha256', 'secret').update(new Array(10000).fill(item).join('.')).digest('hex')
}

// This fragment is needed only to confirm that, processing a large array,
// We give the opportunity to execute another code.

let interval = setInterval(() => {
console.log('tick!')
if (arr.length === 0) clearInterval(interval)
}, 0)
``````