我正在运行以下代码(我欣赏其中的一些重复代码,需要整理一下):
async function translateRequests(nameOfRequestsArray) {
document.body.style.cursor = "wait";
for (let i = 0; i < nameOfRequestsArray.length; i++) {
//see if nameOfRequestsArray[i] has been translated already and if it has then skip it!
if (nameOfRequestsArray[i].lastUpdated.length != 3) {
//translate requesters
let a = users.map(function(search) { return search.id; }).indexOf(nameOfRequestsArray[i].requester);
nameOfRequestsArray[i].requesterFirstName = users[a].firstName;
nameOfRequestsArray[i].requesterSurname = users[a].surname;
//translate requestees
console.log(nameOfRequestsArray[i]);
console.log('i1: ' + i);
if (nameOfRequestsArray[i].type == 'user') {
let b = users.map(function(search) { return search.id; }).indexOf(nameOfRequestsArray[i].requestee);
nameOfRequestsArray[i].requesteeFirstName = users[b].firstName;
nameOfRequestsArray[i].requesteeSurname = users[b].surname;
} else if (nameOfRequestsArray[i].type == 'team') {
//get teams if they haven't already been downloaded
if (teams.length == 0) {
//get teams
data = JSON.stringify({"active": true});
teams = await makeRequest("POST", "http://127.0.0.1:7071/retrieveteams", data);
console.log(JSON.stringify(teams), nameOfRequestsArray[i].requestee)
}
console.log(nameOfRequestsArray[i]);
console.log('i2: ' + i);
let d = teams.map(function(search) { return search.id; }).indexOf(nameOfRequestsArray[i].requestee);
nameOfRequestsArray[i].requesteeFirstName = teams[d].teamName;
}
//translate categories
let c = categories.map(function(search) { return search.id; }).indexOf(nameOfRequestsArray[i].categoryId);
nameOfRequestsArray[i].categoryName = categories[c].name;
//split up lastUpdated dates
let z = Date.parse(nameOfRequestsArray[i].lastUpdated) - timezoneOffset;
z = new Date(z).toISOString();
nameOfRequestsArray[i].lastUpdated = z.replace('Z','').split('T');
nameOfRequestsArray[i].lastUpdated[2] = nameOfRequestsArray[i].lastUpdated[0].split('-').reverse().join('-');
z = nameOfRequestsArray[i].lastUpdated[2].split('-');
z[1] = z[1].replace('01','Jan');
z[1] = z[1].replace('02','Feb');
z[1] = z[1].replace('03','Mar');
z[1] = z[1].replace('04','Apr');
z[1] = z[1].replace('05','May');
z[1] = z[1].replace('06','Jun');
z[1] = z[1].replace('07','Jul');
z[1] = z[1].replace('08','Aug');
z[1] = z[1].replace('09','Sep');
z[1] = z[1].replace('10','Oct');
z[1] = z[1].replace('11','Nov');
z[1] = z[1].replace('12','Dec');
nameOfRequestsArray[i].lastUpdated[2] = z.join('-');
//translate updates
for (let x in nameOfRequestsArray[i].updates) {
let d = users.map(function(search) { return search.id; }).indexOf(nameOfRequestsArray[i].updates[x].author);
nameOfRequestsArray[i].updates[x].authorFirstName = users[d].firstName;
nameOfRequestsArray[i].updates[x].authorSurname = users[d].surname;
//translate the update dates
let y = Date.parse(nameOfRequestsArray[i].updates[x].date) - timezoneOffset;
y = new Date(y).toISOString();
nameOfRequestsArray[i].updates[x].date = y.replace('Z','').split('T');
nameOfRequestsArray[i].updates[x].date[0] = nameOfRequestsArray[i].updates[x].date[0].split('-').reverse().join('-');
y = nameOfRequestsArray[i].updates[x].date[0].split('-');
y[1] = y[1].replace('01','Jan');
y[1] = y[1].replace('02','Feb');
y[1] = y[1].replace('03','Mar');
y[1] = y[1].replace('04','Apr');
y[1] = y[1].replace('05','May');
y[1] = y[1].replace('06','Jun');
y[1] = y[1].replace('07','Jul');
y[1] = y[1].replace('08','Aug');
y[1] = y[1].replace('09','Sep');
y[1] = y[1].replace('10','Oct');
y[1] = y[1].replace('11','Nov');
y[1] = y[1].replace('12','Dec');
nameOfRequestsArray[i].updates[x].date[2] = y.join('-');
}
}
}
document.body.style.cursor = "initial";
}
这将在控制台中输出以下内容:
{type: "team", requester: "2f3e7466-28c7-1c90-cfaf-3dc8370d5dd3", requestee: "88edea02-f145-d79f-f077-e2b6a5db8972", categoryId: "2cb42736-f243-dacb-9961-9e007dc51f29", priority: 1, …}
javascript.js:135 i1: 26
javascript.js:208 type: undefined
javascript.js:146 [{"teamName":"Alpha 1","id":"88edea02-f145-d79f-f077-e2b6a5db8972"}] 2f3e7466-28c7-1c90-cfaf-3dc8370d5dd3
javascript.js:148 {type: "user", requester: "2f3e7466-28c7-1c90-cfaf-3dc8370d5dd3", requestee: "2f3e7466-28c7-1c90-cfaf-3dc8370d5dd3", categoryId: "2cb42736-f243-dacb-9961-9e007dc51f29", priority: 2, …}
javascript.js:149 i2: 26
如您所见(请参阅被请求者和优先级属性),当它到达nameOfRequestsArray [26]时,即使我仍然= 26,该数组的顺序似乎也在改变。我认为数组的意义是除非您通过执行.sort或.reverse显式更改顺序,否则内部数据将保持相同顺序?这导致了我以前从未遇到过的执行问题。
答案 0 :(得分:0)
由于translateRequests是一个异步函数,因此调用它的函数应该在调用下一个函数(在我的情况下是对数组进行排序的函数)之前等待其完成使用,这就是数组内容移位的原因