英文:
How can I loop through this "Object Array"?
问题
这是你要翻译的代码部分:
当我在控制台中输出数组时,它看起来像这样:
[![在此输入图像描述][1]][1]
[1]: https://i.stack.imgur.com/7ZVr3.png
for (let index = 0; index < employeeList.length; index++)
这个for循环不起作用,因为长度为0。我尝试过不同的方法,比如
Object.keys(myObj).length;
或者其他Object的方法。
我不知道如何遍历这个对象数组。
我也不知道如何通过以下方式访问索引值:
for (var key in employeeList)
并且例如获取employeeId。
你能帮助我吗?:)
完整代码:
public selectWorkingHourListForCurrentMonthAndEveryEmployee(
month: Date
): Observable<WorkingHours[]> {
const firstDay = new Date(month);
firstDay.setDate(1);
const lastDay = a Date(
firstDay.getFullYear(),
firstDay.getMonth() + 1,
0
);
const employeeList: Employee[] = [];
let workingHoursSummedUp: WorkingHours[] = [];
this.employeeService.getAllEmployees().then((res) => {
employeeList.push(...res);
});
console.log(employeeList)
if (employeeList) {
for (let index = 0; index < employeeList.length; index++) {
this.workingHoursService
.getWorkingHours(
employeeList[index].employeeId,
firstDay,
lastDay,
true
)
.then((res) => {
workingHoursSummedUp.push(...res);
});
}
}
英文:
When I output the Array in my console, it looks like this:
for (let index = 0; index < employeeList.length; index++)
this for loop doesn't work, because length is 0. I tried different methods, like
Object.keys(myObj).length;
or others from Object.
I don't know how to iterate through this Object Array.
I also don't get how I could access an index value through:
for (var key in employeeList)
and get for example the employeeId.
Could you help me?:)
Full Code :
public selectWorkingHourListForCurrentMonthAndEveryEmployee(
month: Date
): Observable<WorkingHours[]> {
const firstDay = new Date(month);
firstDay.setDate(1);
const lastDay = new Date(
firstDay.getFullYear(),
firstDay.getMonth() + 1,
0
);
const employeeList: Employee[] = [];
let workingHoursSummedUp: WorkingHours[] = [];
this.employeeService.getAllEmployees().then((res) => {
employeeList.push(...res);
});
console.log(employeeList)
if (employeeList) {
for (let index = 0; index < employeeList.length; index++) {
this.workingHoursService
.getWorkingHours(
employeeList[index].employeeId,
firstDay,
lastDay,
true
)
.then((res) => {
workingHoursSummedUp.push(...res);
});
}
}
答案1
得分: 1
有两件事情你必须知道:
- 默认情况下,代码不会停止并等待 promises 完成。
- 时光不能倒流。
所以像这样做是行不通的:
const x = []
doSomething().then(res => x.push(...res))
console.log(x)
在上面的代码中,console.log(x)
会在 then
中的函数之前发生。
最简单的解决方案是使用 async
和 await
:
public async selectWorkingHourListForCurrentMonthAndEveryEmployee(
month: Date
): Observable<WorkingHours[]> {
const firstDay = new Date(month);
firstDay.setDate(1);
const lastDay = new Date(firstDay.getFullYear(), firstDay.getMonth() + 1, 0);
let workingHoursSummedUp: WorkingHours[] = [];
const employeeList: Employee[] = this.employeeService.getAllEmployees();
for (let index = 0; index < employeeList.length; index++) {
const res = await this.workingHoursService.getWorkingHours(
employeeList[index].employeeId,
firstDay,
lastDay,
true
);
workingHoursSummedUp.push(...res);
}
console.log(workingHoursSummedUp);
}
英文:
There are two things you have to know:
- By default code will not stop and wait for promises
- There is no time travel
So doing things like this won't work:
const x = []
doSomething().then(res => x.push(...res))
console.log(x)
In the above code console.log(x)
will happen before the function in then
The easiest solution is to use async
and await
public async selectWorkingHourListForCurrentMonthAndEveryEmployee(
month: Date
): Observable<WorkingHours[]> {
const firstDay = new Date(month);
firstDay.setDate(1);
const lastDay = new Date(firstDay.getFullYear(), firstDay.getMonth() + 1, 0);
let workingHoursSummedUp: WorkingHours[] = [];
const employeeList: Employee[] = this.employeeService.getAllEmployees();
for (let index = 0; index < employeeList.length; index++) {
const res = await this.workingHoursService.getWorkingHours(
employeeList[index].employeeId,
firstDay,
lastDay,
true
);
workingHoursSummedUp.push(...res);
}
console.log(workingHoursSummedUp);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论