如何循环遍历这个“对象数组”?

huangapple go评论62阅读模式
英文:

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

有两件事情你必须知道:

  1. 默认情况下,代码不会停止并等待 promises 完成。
  2. 时光不能倒流。

所以像这样做是行不通的:

const x = []
doSomething().then(res => x.push(...res))
console.log(x)

在上面的代码中,console.log(x) 会在 then 中的函数之前发生。

最简单的解决方案是使用 asyncawait

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:

  1. By default code will not stop and wait for promises
  2. There is no time travel

So doing things like this won't work:

const x = []
doSomething().then(res =&gt; 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&lt;WorkingHours[]&gt; {
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 &lt; employeeList.length; index++) {
const res = await this.workingHoursService.getWorkingHours(
employeeList[index].employeeId,
firstDay,
lastDay,
true
);
workingHoursSummedUp.push(...res);
}
console.log(workingHoursSummedUp);
}

huangapple
  • 本文由 发表于 2023年2月23日 21:59:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75545802.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定