I have a long list that I want to separate five by five and change the name of the value and send the first 5 to the Api. If the result is success, the next 5 will be sent.
This is what I did
let array= [{v1:0,v2:1,v3:2,v4:3},{v1:4,v2:5,v3:6,v4:7},{v1:8,v2:9,v3:10,v4:11},{v1:12,v2:13,v3:14,v4:15}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
and:
let chunkSize = 5;
const chunkResult = array.reduce((resultArray, item, index) => {
const chunkIndex = Math.floor(index / chunkSize);
if (!resultArray[chunkIndex]) {
resultArray[chunkIndex] = []; // start a new chunk
}
resultArray[chunkIndex].push(item);
return resultArray;
}, []);
/// result chunkResult --> [[{v1:0,v2:1,v3:2,v4:3},{v1:4,v2:5,v3:6,v4:7},{v1:8,v2:9,v3:10,v4:11},{v1:12,v2:13,v3:14,v4:15},{v1:12,v2:13,v3:14,v4:15}], Array(5), Array(5), Array(5), Array(5), Array(5), Array(5)]
and:
var j = 0;
var check = false;
do {
const element = chunkResult[j];
let convertArray = element.map(
({
v1: tafzilCode,
v2: nationalCode,
v3: remain,
v4: credit,
}) => ({
tafzilCode,
nationalCode,
remain,
credit,
})
);
//result convertArray ->[{tafzilCode:0,nationalCode:1,remain:2,credit:3},{...},{...},{..},{..}]
props.service(
convertArray,
(status, result) => {
////console.log(result?.success)-->true
if (result?.success) {
check=true;
j++;
} else {
check=false;
}
}
);
///console.log(check)-->false
} while (j < chunkResult.length && check);
But what happens is that even though success is true, the check is not updated And only the first 5 will be sent.Where is my mistake?
1